1#!/usr/bin/env python 2# 3# USAGE: test_usdt.py 4# 5# Copyright 2018 Facebook, Inc 6# Licensed under the Apache License, Version 2.0 (the "License") 7 8from __future__ import print_function 9from bcc import BPF 10from unittest import main, skipUnless, TestCase 11from subprocess import Popen, PIPE 12import distutils.version 13import os 14 15def kernel_version_ge(major, minor): 16 # True if running kernel is >= X.Y 17 version = distutils.version.LooseVersion(os.uname()[2]).version 18 if version[0] > major: 19 return True 20 if version[0] < major: 21 return False 22 if minor and version[1] < minor: 23 return False 24 return True 25 26class TestFreeLLVMMemory(TestCase): 27 def getRssFile(self): 28 p = Popen(["cat", "/proc/" + str(os.getpid()) + "/status"], 29 stdout=PIPE) 30 rss = None 31 unit = None 32 for line in p.stdout.readlines(): 33 if (line.find(b'RssFile') >= 0): 34 rss = line.split(b' ')[-2] 35 unit = line.split(b' ')[-1].rstrip() 36 break 37 38 return [rss, unit] 39 40 @skipUnless(kernel_version_ge(4,5), "requires kernel >= 4.5") 41 def testFreeLLVMMemory(self): 42 text = "int test() { return 0; }" 43 b = BPF(text=text) 44 45 # get the RssFile before freeing bcc memory 46 [rss1, unit1] = self.getRssFile() 47 self.assertTrue(rss1 != None) 48 49 # free the bcc memory 50 self.assertTrue(b.free_bcc_memory() == 0) 51 52 # get the RssFile after freeing bcc memory 53 [rss2, unit2] = self.getRssFile() 54 self.assertTrue(rss2 != None) 55 56 self.assertTrue(unit1 == unit2) 57 58 print("Before freeing llvm memory: RssFile: ", rss1, unit1) 59 print("After freeing llvm memory: RssFile: ", rss2, unit2) 60 self.assertTrue(rss1 > rss2) 61 62if __name__ == "__main__": 63 main() 64