forked from phiran/cputhrottle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manip.cc
209 lines (167 loc) · 5.79 KB
/
manip.cc
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "manip.h"
#include <boost/lexical_cast.hpp>
#include <mach/mach_init.h>
#include <mach/mach_port.h>
#include <mach/task_info.h>
#include <mach/thread_act.h>
#include <mach/vm_map.h>
#include <signal.h>
Process::Manipulator::Manipulator()
{
}
Process::Manipulator::~Manipulator()
{
pidSetType::iterator i = _attachedPids.begin();
while(i != _attachedPids.end())
{
try
{
i = detachIter(i);
}
catch(ManipulatorException &e)
{
break; // bail
}
}
}
void Process::Manipulator::attach(pid_t pid_)
{
kern_return_t res;
task_t task;
if((res = task_for_pid(mach_task_self(), pid_, &task)) != KERN_SUCCESS)
{
throw ManipulatorException("Error on task_for_pid of pid " +
boost::lexical_cast<std::string>(pid_) +
", res = " +
boost::lexical_cast<std::string>(res));
}
if(task_suspend(task))
{
throw ManipulatorException("Error on task_suspend of pid " +
boost::lexical_cast<std::string>(pid_));
}
_attachedPids.insert(pidSetType::value_type(pid_, task));
}
void Process::Manipulator::detach(pid_t pid_)
{
pidSetType::iterator i = _attachedPids.find(pid_);
if(i == _attachedPids.end())
{
throw ManipulatorException("Not attached to pid " + boost::lexical_cast<std::string>(pid_));
}
detachIter(i);
}
Process::Manipulator::pidSetType::iterator
Process::Manipulator::detachIter(const pidSetType::iterator &iter_)
{
resumeIter(iter_);
pidSetType::iterator iterCopy(iter_);
_attachedPids.erase(iterCopy++);
return iterCopy;
}
void Process::Manipulator::singleStep(pid_t pid_)
{
throw ManipulatorException("Not implemented yet.");
}
void Process::Manipulator::continueRunning(pid_t pid_)
{
throw ManipulatorException("Not implemented yet.");
}
void Process::Manipulator::suspend(pid_t pid_)
{
pidSetType::iterator i = _attachedPids.find(pid_);
if(i == _attachedPids.end())
{
throw ManipulatorException("Not attached to pid " + boost::lexical_cast<std::string>(pid_));
}
suspendIter(i);
}
void Process::Manipulator::resume(pid_t pid_)
{
pidSetType::iterator i = _attachedPids.find(pid_);
if(i == _attachedPids.end())
{
throw ManipulatorException("Not attached to pid " + boost::lexical_cast<std::string>(pid_));
}
resumeIter(i);
}
void Process::Manipulator::sample(pid_t pid_,
double *user_time, double *system_time, double *percent)
{
pidSetType::iterator iter = _attachedPids.find(pid_);
if(iter == _attachedPids.end())
{
throw ManipulatorException("Not attached to pid " + boost::lexical_cast<std::string>(pid_));
}
kill(pid_, SIGSTOP);
kern_return_t error;
struct task_basic_info t_info;
thread_array_t th_array;
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT, th_count;
size_t i;
double my_user_time = 0, my_system_time = 0, my_percent = 0;
if ((error = task_info(iter->second, TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count)) != KERN_SUCCESS)
{
throw ManipulatorException("Error on task_info of pid " + boost::lexical_cast<std::string>(pid_));
}
if ((error = task_threads(iter->second, &th_array, &th_count)) != KERN_SUCCESS)
{
throw ManipulatorException("Error on task_threads of pid " + boost::lexical_cast<std::string>(pid_));
}
// sum time for live threads
for (i = 0; i < th_count; i++)
{
double th_user_time, th_system_time, th_percent;
// if ((error = AGGetMachThreadCPUUsage(th_array[i], &th_user_time, &th_system_time, &th_percent)) != KERN_SUCCESS)
// break;
struct thread_basic_info th_info;
mach_msg_type_number_t th_info_count = THREAD_BASIC_INFO_COUNT;
if ((error = thread_info(th_array[i], THREAD_BASIC_INFO,
(thread_info_t)&th_info, &th_info_count)) != KERN_SUCCESS)
{
throw ManipulatorException("Error on thread_info of pid " + boost::lexical_cast<std::string>(pid_));
}
th_user_time = th_info.user_time.seconds + th_info.user_time.microseconds / 1e6;
th_system_time = th_info.system_time.seconds + th_info.system_time.microseconds / 1e6;
th_percent = (double)th_info.cpu_usage / TH_USAGE_SCALE;
my_user_time += th_user_time;
my_system_time += th_system_time;
my_percent += th_percent;
}
// destroy thread array
for (i = 0; i < th_count; i++)
{
mach_port_deallocate(mach_task_self(), th_array[i]);
}
vm_deallocate(mach_task_self(), (vm_address_t)th_array, sizeof(thread_t) * th_count);
// check last error
if (error != KERN_SUCCESS)
{
throw ManipulatorException("Error in collecting cpu sample of pid " + boost::lexical_cast<std::string>(pid_));
}
// add time for dead threads
my_user_time += t_info.user_time.seconds + t_info.user_time.microseconds / 1e6;
my_system_time += t_info.system_time.seconds + t_info.system_time.microseconds / 1e6;
if (user_time != NULL) *user_time = my_user_time;
if (system_time != NULL) *system_time = my_system_time;
if (percent != NULL) *percent = my_percent;
kill(pid_, SIGCONT);
}
void
Process::Manipulator::resumeIter(const pidSetType::iterator &iter_)
{
if(task_resume(iter_->second))
{
throw ManipulatorException("Error on task_resume of pid " +
boost::lexical_cast<std::string>(iter_->first));
}
}
void
Process::Manipulator::suspendIter(const pidSetType::iterator &iter_)
{
if(task_suspend(iter_->second))
{
throw ManipulatorException("Error on task_suspend of pid " +
boost::lexical_cast<std::string>(iter_->first));
}
}