f0b7af064d6a6cd4cd954c88aff14f5d38e29e57
2 * Copyright (C) 2011 Julien Desfossez
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 struct processtop
*find_process_tid(struct lttngtop
*ctx
, int tid
, char *comm
)
26 struct processtop
*tmp
;
28 for (i
= 0; i
< ctx
->process_table
->len
; i
++) {
29 tmp
= g_ptr_array_index(ctx
->process_table
, i
);
30 if (tmp
&& tmp
->tid
== tid
)
36 struct processtop
* add_proc(struct lttngtop
*ctx
, int tid
, char *comm
,
37 unsigned long timestamp
)
39 struct processtop
*newproc
;
41 /* if the PID already exists, we just rename the process */
42 /* FIXME : need to integrate with clone/fork/exit to be accurate */
43 newproc
= find_process_tid(ctx
, tid
, comm
);
45 newproc
= g_new0(struct processtop
, 1);
47 newproc
->birth
= timestamp
;
48 newproc
->process_files_table
= g_ptr_array_new();
49 newproc
->files_history
= g_new0(struct file_history
, 1);
50 newproc
->files_history
->next
= NULL
;
51 newproc
->totalfileread
= 0;
52 newproc
->totalfilewrite
= 0;
53 newproc
->fileread
= 0;
54 newproc
->filewrite
= 0;
55 newproc
->syscall_info
= NULL
;
56 newproc
->threads
= g_ptr_array_new();
57 newproc
->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
58 g_ptr_array_add(ctx
->process_table
, newproc
);
60 newproc
->comm
= strdup(comm
);
65 struct processtop
* update_proc(struct processtop
* proc
, int pid
, int tid
,
72 if (strcmp(proc
->comm
, comm
) != 0) {
74 proc
->comm
= strdup(comm
);
81 * This function just sets the time of death of a process.
82 * When we rotate the cputime we remove it from the process list.
84 void death_proc(struct lttngtop
*ctx
, int tid
, char *comm
,
85 unsigned long timestamp
)
87 struct processtop
*tmp
;
88 tmp
= find_process_tid(ctx
, tid
, comm
);
89 if (tmp
&& strcmp(tmp
->comm
, comm
) == 0)
90 tmp
->death
= timestamp
;
93 struct processtop
* get_proc(struct lttngtop
*ctx
, int tid
, char *comm
,
94 unsigned long timestamp
)
96 struct processtop
*tmp
;
97 tmp
= find_process_tid(ctx
, tid
, comm
);
98 if (tmp
&& strcmp(tmp
->comm
, comm
) == 0)
100 return add_proc(ctx
, tid
, comm
, timestamp
);
103 void add_thread(struct processtop
*parent
, struct processtop
*thread
)
106 struct processtop
*tmp
;
108 for (i
= 0; i
< parent
->threads
->len
; i
++) {
109 tmp
= g_ptr_array_index(parent
->threads
, i
);
113 g_ptr_array_add(parent
->threads
, thread
);
116 struct cputime
* add_cpu(int cpu
)
118 struct cputime
*newcpu
;
120 newcpu
= g_new0(struct cputime
, 1);
122 newcpu
->current_task
= NULL
;
123 newcpu
->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
125 g_ptr_array_add(lttngtop
.cpu_table
, newcpu
);
129 struct cputime
* get_cpu(int cpu
)
134 for (i
= 0; i
< lttngtop
.cpu_table
->len
; i
++) {
135 tmp
= g_ptr_array_index(lttngtop
.cpu_table
, i
);
144 * At the end of a sampling period, we need to display the cpu time for each
145 * process and to reset it to zero for the next period
147 void rotate_cputime(unsigned long end
)
151 unsigned long elapsed
;
153 for (i
= 0; i
< lttngtop
.cpu_table
->len
; i
++) {
154 tmp
= g_ptr_array_index(lttngtop
.cpu_table
, i
);
155 elapsed
= end
- tmp
->task_start
;
156 if (tmp
->current_task
) {
157 tmp
->current_task
->totalcpunsec
+= elapsed
;
158 tmp
->current_task
->threadstotalcpunsec
+= elapsed
;
159 if (tmp
->current_task
->pid
!= tmp
->current_task
->tid
&&
160 tmp
->current_task
->threadparent
) {
161 tmp
->current_task
->threadparent
->threadstotalcpunsec
+= elapsed
;
164 tmp
->task_start
= end
;
168 void reset_perf_counter(gpointer key
, gpointer value
, gpointer user_data
)
170 ((struct perfcounter
*) value
)->count
= 0;
173 void copy_perf_counter(gpointer key
, gpointer value
, gpointer new_table
)
175 struct perfcounter
*newperf
;
177 newperf
= g_new0(struct perfcounter
, 1);
178 newperf
->count
= ((struct perfcounter
*) value
)->count
;
179 newperf
->visible
= ((struct perfcounter
*) value
)->visible
;
180 newperf
->sort
= ((struct perfcounter
*) value
)->sort
;
181 g_hash_table_insert((GHashTable
*) new_table
, strdup(key
), newperf
);
184 void rotate_perfcounter() {
186 struct processtop
*tmp
;
187 for (i
= 0; i
< lttngtop
.process_table
->len
; i
++) {
188 tmp
= g_ptr_array_index(lttngtop
.process_table
, i
);
189 g_hash_table_foreach(tmp
->perf
, reset_perf_counter
, NULL
);
193 void cleanup_processtop()
196 struct processtop
*tmp
;
197 struct files
*tmpf
; /* a temporary file */
199 for (i
= 0; i
< lttngtop
.process_table
->len
; i
++) {
200 tmp
= g_ptr_array_index(lttngtop
.process_table
, i
);
201 tmp
->totalcpunsec
= 0;
202 tmp
->threadstotalcpunsec
= 0;
206 for (j
= 0; j
< tmp
->process_files_table
->len
; j
++) {
207 tmpf
= g_ptr_array_index(tmp
->process_files_table
, j
);
216 struct lttngtop
* get_copy_lttngtop(unsigned long start
, unsigned long end
)
220 struct lttngtop
*dst
;
221 struct processtop
*tmp
, *tmp2
, *new;
222 struct cputime
*tmpcpu
, *newcpu
;
223 struct files
*tmpfile
, *newfile
;
225 dst
= g_new0(struct lttngtop
, 1);
228 dst
->process_table
= g_ptr_array_new();
229 dst
->files_table
= g_ptr_array_new();
230 dst
->cpu_table
= g_ptr_array_new();
231 dst
->perf_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
235 g_hash_table_foreach(lttngtop
.perf_list
, copy_perf_counter
, dst
->perf_list
);
236 for (i
= 0; i
< lttngtop
.process_table
->len
; i
++) {
237 tmp
= g_ptr_array_index(lttngtop
.process_table
, i
);
238 new = g_new0(struct processtop
, 1);
240 memcpy(new, tmp
, sizeof(struct processtop
));
241 new->threads
= g_ptr_array_new();
242 new->comm
= strdup(tmp
->comm
);
243 new->process_files_table
= g_ptr_array_new();
244 new->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
245 g_hash_table_foreach(tmp
->perf
, copy_perf_counter
, new->perf
);
247 /* compute the stream speed */
248 if (end
- start
!= 0) {
249 time
= (end
- start
) / NSEC_PER_SEC
;
250 new->fileread
= new->fileread
/(time
);
251 new->filewrite
= new->filewrite
/(time
);
254 for (j
= 0; j
< tmp
->process_files_table
->len
; j
++) {
255 tmpfile
= g_ptr_array_index(tmp
->process_files_table
, j
);
257 newfile
= malloc(sizeof(struct files
));
259 if (tmpfile
!= NULL
) {
260 memcpy(newfile
, tmpfile
, sizeof(struct files
));
261 newfile
->name
= strdup(tmpfile
->name
);
263 g_ptr_array_add(new->process_files_table
,
265 g_ptr_array_add(dst
->files_table
, newfile
);
267 g_ptr_array_add(new->process_files_table
, NULL
);
268 g_ptr_array_add(dst
->files_table
, NULL
);
271 * if the process died during the last period, we remove all
272 * files associated with if after the copy
274 if (tmp
->death
> 0 && tmp
->death
< end
) {
275 g_ptr_array_remove(tmp
->process_files_table
, tmpfile
);
279 g_ptr_array_add(dst
->process_table
, new);
282 * if the process died during the last period, we remove it from
283 * the current process list after the copy
285 if (tmp
->death
> 0 && tmp
->death
< end
) {
286 g_ptr_array_remove(lttngtop
.process_table
, tmp
);
287 /* FIXME : TRUE does not mean clears the object in it */
288 g_ptr_array_free(tmp
->threads
, TRUE
);
290 g_ptr_array_free(tmp
->process_files_table
, TRUE
);
291 /* FIXME : clear elements */
292 g_hash_table_destroy(tmp
->perf
);
296 rotate_perfcounter();
298 for (i
= 0; i
< lttngtop
.cpu_table
->len
; i
++) {
299 tmpcpu
= g_ptr_array_index(lttngtop
.cpu_table
, i
);
300 newcpu
= g_new0(struct cputime
, 1);
301 memcpy(newcpu
, tmpcpu
, sizeof(struct cputime
));
302 newcpu
->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
303 g_hash_table_foreach(tmpcpu
->perf
, copy_perf_counter
, newcpu
->perf
);
305 * note : we don't care about the current process pointer in the copy
306 * so the reference is invalid after the memcpy
308 g_ptr_array_add(dst
->cpu_table
, newcpu
);
310 /* FIXME : better algo */
311 /* create the threads index if required */
312 for (i
= 0; i
< dst
->process_table
->len
; i
++) {
313 tmp
= g_ptr_array_index(dst
->process_table
, i
);
314 if (tmp
->pid
== tmp
->tid
) {
315 for (j
= 0; j
< dst
->process_table
->len
; j
++) {
316 tmp2
= g_ptr_array_index(dst
->process_table
, j
);
317 if (tmp2
->pid
== tmp
->pid
) {
318 tmp2
->threadparent
= tmp
;
319 g_ptr_array_add(tmp
->threads
, tmp2
);
325 // update_global_stats(dst);
326 cleanup_processtop();
This page took 0.035911 seconds and 4 git commands to generate.