From 68b4a940b787764567b28cddc91f8064a07b6b6e Mon Sep 17 00:00:00 2001 From: lixiaoyong Date: Tue, 26 Nov 2024 23:09:28 +0800 Subject: [PATCH 1/2] Add function __check_and_execute_perf_steps --- category/get_perf_info.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/category/get_perf_info.py b/category/get_perf_info.py index 840e4f2..fb4e8e0 100644 --- a/category/get_perf_info.py +++ b/category/get_perf_info.py @@ -269,17 +269,23 @@ class Perf(): Logger().debug("write perf flame svg info error") return False - # perf main function - @GlobalCall.monitor_info_thread_pool.threaded_pool - def do_perf_collect(self): - Logger().info("Perf数据采集开始") + def __check_and_execute_perf_steps(self): try: self.__set_perf_parameter() - if self.__check_perf_parameter(): - if self.__get_perf_collect(): - self.__do_perf_flamegraph() + if not self.__check_perf_parameter(): + return False + if not self.__get_perf_collect(): + return False + self.__do_perf_flamegraph() except Exception as e: Logger().debug("do perf collect error: {}".format(e)) + return True + + # perf main function + @GlobalCall.monitor_info_thread_pool.threaded_pool + def do_perf_collect(self): + Logger().info("Perf数据采集开始") + self.__check_and_execute_perf_steps() Logger().info("Perf数据采集结束") -- Gitee From 08939d14cb34ac02c1fbd57a02b2d7d12fbb6418 Mon Sep 17 00:00:00 2001 From: lixiaoyong Date: Tue, 26 Nov 2024 23:23:43 +0800 Subject: [PATCH 2/2] Optimize function cmd_run --- common/command.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/common/command.py b/common/command.py index 2abbe6f..9d9ef29 100644 --- a/common/command.py +++ b/common/command.py @@ -10,6 +10,8 @@ from common.log import Logger from common.file import FileOperation class Command: + LANG = 'en_US.UTF-8' + BASH = "/bin/sh" @staticmethod def call(cmd, caller = ''): @@ -47,27 +49,28 @@ class Command: def cmd_run(cmd, caller = ''): ''' Encapsulate the general RUN function, get the result + return: cmd + '\n' + cmd结果 ''' command_result = '' try: env_c = os.environ - env_c['LANG'] = 'en_US.UTF-8' + env_c['LANG'] = Command.LANG check_cmd = cmd.split() if check_cmd[0] == 'cat' or check_cmd[0] == 'ls': if not os.path.exists(check_cmd[1]): - Logger().error("{} does not exist, cmd_run {} unable to execute".format(check_cmd[1], cmd)) + Logger().error("{} 不存在, cmd_run 命令 {} 无法执行".format(check_cmd[1], cmd)) return command_result if cmd == "top -b -n 3": env = os.environ.copy() env['COLUMNS'] = '132' # 设置输出宽度为132列 - ret = subprocess.Popen(["/bin/sh", "-c", cmd], stdout = subprocess.PIPE , stderr = subprocess.PIPE, env = env) + ret = subprocess.Popen([Command.BASH, "-c", cmd], stdout = subprocess.PIPE , stderr = subprocess.PIPE, env = env) else: - ret = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE , stderr = subprocess.PIPE, env = env_c) + ret = subprocess.Popen([Command.BASH, "-c", cmd], stdout = subprocess.PIPE , stderr = subprocess.PIPE, env = env_c) stdout,stderr = ret.communicate() - if not sys.version_info[0] >= 3: + if sys.version_info[0] < 3: stdout = stdout.replace('\x1b[7l', '') if Command.cmd_check(stdout, stderr, ret.returncode, cmd): command_result = cmd + '\n'+ stdout.decode('utf8') @@ -75,7 +78,7 @@ class Command: return command_result except Exception as err: - Logger().error("An exception occurred when executing [{}]: {}".format(cmd, err)) + Logger().error("001:An exception occurred when executing [{}]: {}".format(cmd, err)) return command_result @staticmethod -- Gitee