xref: /aosp_15_r20/external/autotest/client/profilers/powertop/src/process.c (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1*9c5db199SXin Li /*
2*9c5db199SXin Li  * Copyright 2007, Intel Corporation
3*9c5db199SXin Li  *
4*9c5db199SXin Li  * This file is part of PowerTOP
5*9c5db199SXin Li  *
6*9c5db199SXin Li  * This program file is free software; you can redistribute it and/or modify it
7*9c5db199SXin Li  * under the terms of the GNU General Public License as published by the
8*9c5db199SXin Li  * Free Software Foundation; version 2 of the License.
9*9c5db199SXin Li  *
10*9c5db199SXin Li  * This program is distributed in the hope that it will be useful, but WITHOUT
11*9c5db199SXin Li  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12*9c5db199SXin Li  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13*9c5db199SXin Li  * for more details.
14*9c5db199SXin Li  *
15*9c5db199SXin Li  * You should have received a copy of the GNU General Public License
16*9c5db199SXin Li  * along with this program in a file named COPYING; if not, write to the
17*9c5db199SXin Li  * Free Software Foundation, Inc.,
18*9c5db199SXin Li  * 51 Franklin Street, Fifth Floor,
19*9c5db199SXin Li  * Boston, MA 02110-1301 USA
20*9c5db199SXin Li  *
21*9c5db199SXin Li  * Authors:
22*9c5db199SXin Li  * 	Arjan van de Ven <[email protected]>
23*9c5db199SXin Li  */
24*9c5db199SXin Li 
25*9c5db199SXin Li #include <unistd.h>
26*9c5db199SXin Li #include <stdio.h>
27*9c5db199SXin Li #include <stdlib.h>
28*9c5db199SXin Li #include <string.h>
29*9c5db199SXin Li #include <stdint.h>
30*9c5db199SXin Li #include <sys/types.h>
31*9c5db199SXin Li #include <dirent.h>
32*9c5db199SXin Li #include <sys/types.h>
33*9c5db199SXin Li #include <signal.h>
34*9c5db199SXin Li 
35*9c5db199SXin Li #include "powertop.h"
36*9c5db199SXin Li 
37*9c5db199SXin Li char process_to_kill[1024];
38*9c5db199SXin Li 
fancy_kill(void)39*9c5db199SXin Li static void fancy_kill(void)
40*9c5db199SXin Li {
41*9c5db199SXin Li 	FILE *file;
42*9c5db199SXin Li 	char line[2048];
43*9c5db199SXin Li 	char *tokill;
44*9c5db199SXin Li 	int pid = 0;
45*9c5db199SXin Li 	tokill = &process_to_kill[1];
46*9c5db199SXin Li 
47*9c5db199SXin Li 	file = popen(" ps -A -o pid,command", "r");
48*9c5db199SXin Li 	if (!file)
49*9c5db199SXin Li 		return;
50*9c5db199SXin Li 	while (!feof(file)) {
51*9c5db199SXin Li 		memset(line, 0, 2048);
52*9c5db199SXin Li 		if (fgets(line, 2047, file)==NULL)
53*9c5db199SXin Li 			break;
54*9c5db199SXin Li 		if (!strstr(line, tokill))
55*9c5db199SXin Li 			continue;
56*9c5db199SXin Li 		pid = strtoul(line, NULL, 10);
57*9c5db199SXin Li 
58*9c5db199SXin Li 	}
59*9c5db199SXin Li 	pclose(file);
60*9c5db199SXin Li 	if (pid<2)
61*9c5db199SXin Li 		return;
62*9c5db199SXin Li 	kill(pid, SIGTERM);
63*9c5db199SXin Li }
64*9c5db199SXin Li 
do_kill(void)65*9c5db199SXin Li void do_kill(void)
66*9c5db199SXin Li {
67*9c5db199SXin Li 	char line[2048];
68*9c5db199SXin Li 
69*9c5db199SXin Li 	if (process_to_kill[0] == '-') {
70*9c5db199SXin Li 		fancy_kill();
71*9c5db199SXin Li 	} else {
72*9c5db199SXin Li 		sprintf(line, "killall %s &> /dev/null", process_to_kill);
73*9c5db199SXin Li 		system(line);
74*9c5db199SXin Li 	}
75*9c5db199SXin Li }
76*9c5db199SXin Li 
suggest_process_death(char * process_match,char * tokill,struct line * slines,int linecount,double minwakeups,char * comment,int weight)77*9c5db199SXin Li void suggest_process_death(char *process_match, char *tokill, struct line *slines, int linecount, double minwakeups, char *comment, int weight)
78*9c5db199SXin Li {
79*9c5db199SXin Li 	int i;
80*9c5db199SXin Li 
81*9c5db199SXin Li 	for (i = 0; i < linecount; i++) {
82*9c5db199SXin Li 		if (slines[i].string && strstr(slines[i].string, process_match)) {
83*9c5db199SXin Li 			char hotkey_string[300];
84*9c5db199SXin Li 			sprintf(hotkey_string, _(" K - kill %s "), tokill);
85*9c5db199SXin Li 			strcpy(process_to_kill, tokill);
86*9c5db199SXin Li 			if (minwakeups < slines[i].count)
87*9c5db199SXin Li 				add_suggestion(comment, weight, 'K' , hotkey_string, do_kill);
88*9c5db199SXin Li 			break;
89*9c5db199SXin Li 		}
90*9c5db199SXin Li 	}
91*9c5db199SXin Li 	fflush(stdout);
92*9c5db199SXin Li }
93