-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessGroup.cpp
More file actions
164 lines (149 loc) · 3.9 KB
/
ProcessGroup.cpp
File metadata and controls
164 lines (149 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "ProcessGroup.h"
#include <sys/time.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int ProcessGroup::sMaxPss = 0;
ProcessGroup::ProcessGroup(const char* process_name) {
// If constructor is done, it contains 'Process'es.
// Each Process has attributes which has already set,
// and its attributes are "Process Name", "Total",
// "Text", "Data", and so on.
// Set the name of the process group
this->mProcessName = new char[strlen(process_name) + 1];
memcpy(this->mProcessName, process_name, strlen(process_name) + 1);
// Set pid list for given process name
this->setProcesses();
}
ProcessGroup::~ProcessGroup() {
// Release process name string
delete[] this->mProcessName;
// Release each elements of mProcesses
std::vector<Process*>::iterator it;
for(it = this->mProcesses.begin();
it != this->mProcesses.end();
it++) {
delete (*it);
}
// Clear entire mProcesses list
this->mProcesses.clear();
}
const char* ProcessGroup::getProcessName() {
return this->mProcessName;
}
void ProcessGroup::getTotalMessage(char* buffer, int bufferSize) {
if(this->getSize() <= 0) {
snprintf(buffer, bufferSize, "");
return;
}
int totalText = 0;
int totalData = 0;
// int totalBssHeap = 0;
int totalStack = 0;
int totalSharedLibrary = 0;
int totalPss = 0;
std::vector<Process*>::iterator it;
for(it = this->mProcesses.begin();
it != this->mProcesses.end();
it++) {
Process* p = (*it);
totalText += p->getText();
totalData += p->getData();
// totalBssHeap += p->getBssHeap();
totalStack += p->getStack();
totalSharedLibrary += p->getSharedLibrary();
totalPss += p->getPss();
int total = p->getText() + p->getData() + p->getStack() + p->getSharedLibrary();
}
int avgSharedLibrary = totalSharedLibrary / this->getSize();
struct timeval tv;
long long timestamp_us;
gettimeofday(&tv, NULL);
timestamp_us = tv.tv_sec * 1000 * 1000 + tv.tv_usec;
if(totalPss > sMaxPss)
sMaxPss = totalPss;
snprintf(buffer, bufferSize, "%lld %d %d %d %d %d %d",
timestamp_us,
totalText, totalData, totalStack, avgSharedLibrary, totalPss, sMaxPss);
return;
}
Process& ProcessGroup::getProcess(int index) {
return *(this->mProcesses[index]);
}
int ProcessGroup::getSize() {
return this->mProcesses.size();
}
void ProcessGroup::setProcesses() {
int pd[2];
int pid_first;
char* given_pname = this->mProcessName; // given process name
// make pipe
if(pipe(pd) == -1) {
printf("pipe creation failed\n");
}
// fork first process
pid_first = fork();
if(pid_first < 0) {
// fork error
printf("Fork error\n");
return;
} else if(pid_first == 0) {
int result;
// child process
dup2(pd[1], 1);
close(pd[0]);
close(pd[1]);
// execute "ps -A"
result = execlp("ps", "ps", "-A", NULL);
if(result == -1) {
printf("Command exec error\n");
}
exit(127);
} else {
// mother process
dup2(pd[0], 0);
close(pd[0]);
close(pd[1]);
// read the result of "ps -A"
char buffer[1024];
int linenum = 0;
while(fgets(buffer, 1024, stdin)) {
linenum++;
if(linenum == 1) continue; // skip first line
int read_pid = -1;
char read_pname[1024];
int index = 0;
char* ptok = strtok(buffer, " ");
while(ptok != NULL){
switch(index) {
case 0:
read_pid = atoi(ptok);
break;
case 3:
memcpy(read_pname, ptok, strlen(ptok) + 1);
break;
}
index++;
ptok = strtok(NULL, " ");
}
if(read_pid == -1)
continue;
// Read PID & Process Name is here
if(strncmp(read_pname, given_pname, strlen(given_pname)) == 0
&& strlen(read_pname) == strlen(given_pname)) {
// Add new process snapshot class to the list
this->mProcesses.push_back(new Process(read_pid));
}
}
// finish piping
int status;
close(pd[0]);
close(pd[1]);
waitpid(pid_first, &status, WUNTRACED); // wait for child process
}
}