In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Python script to achieve memory leak testing method and solution process, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
My next article provides a lightweight memory leak testing method and its python implementation. This method has been tested in the acceptance testing activities of Lenovo Bamboo system, and it is an easy-to-use and effective memory leak testing method.
Principle of memory leak testing
1. The harm of memory leakage.
Needless to say, the harm of memory leakage will lead to less and less available memory of the system, which will affect the stability of the system running for a long time.
2. Commonly used memory leak testing methods.
Generally speaking, it can be summarized as two ways of thinking:
1) memory allocation and release tool check
Memory testing tools such as valgrind.
2) performance monitoring of Linux system
Such as zabbix and other linux performance monitoring tools, as well as the team developed tools to test the performance of linux.
In method 1, the principle of the tool is to determine whether a memory leak has occurred by checking whether there is a release after the program dynamically allocates memory. The advantage is obvious: when a leak is found, it is easy to locate the specific location of the code where the memory is leaking. But the disadvantage is also obvious: one is that the tool is easy to misjudge, and the other is that the tool interferes with the program compilation process, which makes it very troublesome to use the tool. These two shortcomings are especially easy to feel for black box testers.
In fact, there is a third drawback. As a tester, do you really care about whether there is free in the memory at the end of the program? otherwise. Most processes are supposed to run forever (at least we hope so), and what testers really care about is whether the memory used by the process is basically stable or continues to grow. If it continues to grow, there is a risk of affecting the stability of the system for a long time.
In summary, the Bamboo OS tester summarizes the memory leak testing method according to method 2, and implements the tool script using python.
3. Linux memory management mechanism
There is a lot of information on the Internet about the memory management mechanism of Linux, so we won't talk about it. This article only talks a few words about some basic concepts.
Virtual memory: each process on linux has a virtual address space, which is represented as VSZ in the ps command and VIRT in the top command. This space can be large, and it is common that the VSZ of a single process exceeds the memory of the entire system.
Resident memory: only the memory that is actually accessed is mapped to RAM, which is represented as RSS in the ps command and RES in the top command.
As shown in the following figure, the VSZ of vpp is 99G (you see, wayward. ), RSS is 787m.
(note: the basic unit of memory in the PS command is KB,B for bit)
There is another way to divide the memory used by processes: private memory and shared memory.
As the name implies, private memory is the memory that is exclusive to the process, and shared memory is the memory shared by multiple processes. in general, when multiple processes rely on the same link library, the link library is also mapped to the address space of each process. So, even RSS doesn't really reflect how much memory the process takes up on the system.
To sum up, one of the basic ideas for testing process memory leaks on linux systems is:
Periodically observe the rss and private memory changes of the process through commands such as ps, cat proc/$pid/status, pmap-d $pid, and so on. If both continue to rise, the process is highly suspected of memory leaks. If only one of them is rising, you need to further use the positioning method (or ask for development assistance), it is best to find out the specific reason.
There are many commands to monitor the memory performance of linux. In addition to the commands mentioned above, user-mode processes also monitor smaps,vmstat and other commands, and the kernel generally looks at slabinfo. The detailed usage of these commands is not covered in this article.
Second, the implementation of memory leak detection script in Bamboo system.
1. Design and test method
Depending on the degree of understanding of the system, you can selectively
1) find out where the project allocates memory dynamically by reviewing design documents or historical defects and communicating with developers.
2) Design the test steps, which should make the final state the same as the initial state. For example, after creating a router ospf instance, initializing the instance, allocating resources and interacting with protocols, the instance is deleted and the system returns to its original state.
3) execute the test steps repeatedly and observe the memory changes through the command of linux. If rss and private memory continue to rise, there is a suspected memory leak
4) locate (or coordinate development to locate) problems, which can be located by comparing the details of pmap commands, gdb debugging, valgrind, or other tools.
2. Implement the memory copy machine test script
The memory copying machine script tool consists of two parts, memMonitor and mytest.
MemMonitor is a working framework that relies on the automated test platform of the Bamboo system, which provides bamboos_ssh functions that allow scripts to create an instance of the Bamboo system and execute commands on the system. The version provided by the author obtains the memory information of the system through the command line, and each function obtains the data by analyzing the string, and the reader can realize it by himself. The presentation of memory monitoring results can be cool and easy, and readers can realize it on their own.
The parameter mytest of memMonitor is a function in which the tester designs the test steps (such as creating and destroying ospf instances mentioned above). For the user of the tool, he does not need to care about the internal implementation mechanism of memMonitor, just design his own mytest.
The basic structure of the memMonitor script is as follows.
Import bamboos_ssh
Import re
Import time
''
This use case monitors the memory changes of specified processes in the Bamboo system through the ps and pmap commands provided by linux.
''
# #-Global parameter settings-set detection range-# #
ProcessList = ['. / omu','/bin/ffe/vpp/vpp','l3stackMain','srvc','nse','ospfd','nettool_server'
'infoc','ffe_mgmt','usermgr','nat','dhcpd','ntpd','bgpd','sysrepod']
# # + + #
Def memMonitor (targetSystemIP,psName,repeatRounds,mytest):
RssList = []
PrivateList = []
# #-initialization work-# #
Dut1_ssh = bamboos_ssh.bamboos_ssh (targetSystemIP)
Print ('\ nobtain the ps information completely at the beginning, find the process number of the target process, and get the pmap information of the process')
PsAllInfo = dut1_ssh.exe_cmd (['ps-aux'])
PsID = getPsID (psName,psAllInfo)
PsAllRssStart = getPsAllRss (processList,psAllInfo)
Dut1_ssh.exe_cmd (['pmap-d% s'%psID])
Dut1_ssh.exe_cmd (['exit'])
# #-copy machine process-# #
# # the main loop executes the tester's copy script many times, and obtains rss memory and private memory information after each execution
For i in range (0repeatRoundskeeper 1):
Try:
If 0 = = I:
Print ('\ nget initial memory information')
Else:
Print ('\ n% D-round Test'% I)
Mytest (dut1_ssh)
Print ('\ nThe% d times get memory information'% I)
RssMem = getRssMem (dut1_ssh,psID)
RssList.append (rssMem)
PrivateMem = getPrivateMem (dut1_ssh,psID)
PrivateList.append (int (privateMem))
Dut1_ssh.exe_cmd (['exit'])
Except BaseException ase:
Print (e); print ('% D-round terminated abnormally during test'% I); resultPrint (psName, rssList, privateList); break
# # obtain the ps information and the pmap information of the target process in the last round of testing
Print ('\ ncomplete acquisition of ps information at the end, as well as pmap information of the target process')
PsAllInfo = dut1_ssh.exe_cmd (['ps-aux'])
PsAllRssEnd = getPsAllRss (processList,psAllInfo)
Dut1_ssh.exe_cmd (['pmap-d% s'% psID])
Dut1_ssh.exe_cmd (['exit'])
# #-display the final monitoring result-# #
Print ('= results of the machine test =')
ResultPrint (psName, rssList, privateList)
CompareAllPsRss (processList, psAllRssStart, psAllRssEnd)
# #-Clean up the test environment and end the test-# #
Dut1_ssh.close ()
# # + + #
Def getPsID (psName,psInfo):
# retrieve psID according to PSInfo
# return psID
Def getPsAllRss (psNameList,psAllInfo):
# retrieve the RSS of all ps through psALLInfo and return
# return psRssList
Def getRssMem (dut1_ssh,psID):
# find out the rss memory value of each ps according to the command 'cat / proc/%s/status | grep VmRSS'
# return rssMem
Def getPrivateMem (dut1_ssh,psID):
# find out the private memory value of each ps according to the 'pmap-d% s | grep mapped' command
# return privateMem
Def resultPrint (psName,rssList,privateList):
# output memory monitoring results as needed, which can be cool graphical output or simple output, such as:
Print ('\ nphysical memory footprint trend for process% s is:'% psName)
Print (rssList)
Print (private memory usage trend for process% s is:'% psName)
Print (privateList)
Def compareAllPsRss (processList,psAllRssStart,psAllRssEnd):
# one output to all processes, either graphical or simple
3. Use memory copying machine to test the script
Examples of use:
1) the test students suspected that there was a memory leak in the configuration of the local clock based on the ntp feature, so the main monitoring process was ntpd.
2) the command to be executed repeatedly is defined in the mytest function.
Def mytest (dut1_ssh):
For i in range (0Pol 10):
Dut1_ssh.config (['ntp-service refclock-master 9'])
Time.sleep (2)
Dut1_ssh.config (['no ntp-service refclock-master'])
3) the main program will execute mytest repeatedly and periodically check the rss memory and private memory information of the ntpd process
4) the main program obtains the complete pmap information of a ntpd process at the beginning and end, which is convenient for further location analysis when there is a memory leak in ntpd.
Running result:
The main results are as follows: (the version used by the author does not currently graphically represent the data.)
As you can see, ntpd's rss memory has increased significantly, and private memory has been on the rise. Therefore, ntpd has a strong suspicion of memory leaks.
Because I want to further analyze the changes in memory, I compare the pmap information of ntpd before and after the program execution. You can see that at the end, there are more 4K pages in the pmap message. The tester provided this information to the developer, who quickly found out that the cause of the memory leak was that NTP did not release paging after logging.
On the Python script to achieve memory leak testing methods and solutions to process problems are shared here, I hope the above content can be of some help to you, if you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.