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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
|
/* Sysprof -- Sampling, systemwide CPU profiler
* Copyright 2004, Red Hat, Inc.
* Copyright 2004, 2005, Soeren Sandmann
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdint.h>
#include <glib.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <string.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <errno.h>
#include "stackstash.h"
#include "collector.h"
#include "watch.h"
#include "elfparser.h"
#include "tracker.h"
#include "perf_counter.h"
#include "barrier.h"
#define d_print(...)
#define N_PAGES 32 /* Number of pages in the ringbuffer */
#define N_WAKEUP_EVENTS 150
typedef struct counter_t counter_t;
typedef struct sample_event_t sample_event_t;
typedef struct mmap_event_t mmap_event_t;
typedef struct comm_event_t comm_event_t;
typedef struct exit_event_t exit_event_t;
typedef struct fork_event_t fork_event_t;
typedef union counter_event_t counter_event_t;
static void process_event (Collector *collector,
counter_t *counter,
counter_event_t *event);
struct counter_t
{
Collector * collector;
int fd;
struct perf_counter_mmap_page * mmap_page;
uint8_t * data;
uint64_t tail;
int cpu;
};
struct sample_event_t
{
struct perf_event_header header;
uint64_t ip;
uint32_t pid, tid;
uint64_t n_ips;
uint64_t ips[1];
};
struct comm_event_t
{
struct perf_event_header header;
uint32_t pid, tid;
char comm[1];
};
struct mmap_event_t
{
struct perf_event_header header;
uint32_t pid, tid;
uint64_t addr;
uint64_t len;
uint64_t pgoff;
char filename[1];
};
struct fork_event_t
{
struct perf_event_header header;
uint32_t pid, ppid;
uint32_t tid, ptid;
};
struct exit_event_t
{
struct perf_event_header header;
uint32_t pid, ppid;
uint32_t tid, ptid;
};
union counter_event_t
{
struct perf_event_header header;
mmap_event_t mmap;
comm_event_t comm;
sample_event_t sample;
fork_event_t fork;
exit_event_t exit;
};
struct Collector
{
CollectorFunc callback;
gpointer data;
tracker_t * tracker;
GTimeVal latest_reset;
int prev_samples;
int n_samples;
GList * counters;
};
static int
get_n_cpus (void)
{
return sysconf (_SC_NPROCESSORS_ONLN);
}
static int
sysprof_perf_counter_open (struct perf_counter_attr *attr,
pid_t pid,
int cpu,
int group_fd,
unsigned long flags)
{
#ifndef __NR_perf_counter_open
#define __NR_perf_counter_open 336
#endif
attr->size = sizeof(*attr);
return syscall (__NR_perf_counter_open, attr, pid, cpu, group_fd, flags);
}
static double
timeval_to_ms (const GTimeVal *timeval)
{
return (timeval->tv_sec * G_USEC_PER_SEC + timeval->tv_usec) / 1000.0;
}
static double
time_diff (const GTimeVal *first,
const GTimeVal *second)
{
double first_ms = timeval_to_ms (first);
double second_ms = timeval_to_ms (second);
return first_ms - second_ms;
}
#define RESET_DEAD_PERIOD 250
static gboolean
in_dead_period (Collector *collector)
{
GTimeVal now;
double diff;
g_get_current_time (&now);
diff = time_diff (&now, &collector->latest_reset);
if (diff >= 0.0 && diff < RESET_DEAD_PERIOD)
return TRUE;
return FALSE;
}
static int
get_page_size (void)
{
static int page_size;
static gboolean has_page_size = FALSE;
if (!has_page_size)
{
page_size = getpagesize();
has_page_size = TRUE;
}
return page_size;
}
static void
on_read (gpointer data)
{
counter_t *counter = data;
int mask = (N_PAGES * get_page_size() - 1);
gboolean skip_samples;
Collector *collector;
uint64_t head, tail;
collector = counter->collector;
#if 0
int n_bytes = mask + 1;
int x;
#endif
tail = counter->tail;
head = counter->mmap_page->data_head;
rmb();
if (head < tail)
{
g_warning ("sysprof fails at ring buffers (head %llu, tail %llu\n", head, tail);
tail = head;
}
#if 0
/* Verify that the double mapping works */
x = g_random_int() & mask;
g_assert (*(counter->data + x) == *(counter->data + x + n_bytes));
#endif
skip_samples = in_dead_period (collector);
#if 0
g_print ("n bytes %d\n", head - tail);
#endif
while (head - tail >= sizeof (struct perf_event_header))
{
struct perf_event_header *header = (void *)(counter->data + (tail & mask));
if (header->size > head - tail)
{
/* The kernel did not generate a complete event.
* I don't think that can happen, but we may as well
* be paranoid.
*/
break;
}
if (!skip_samples || header->type != PERF_EVENT_SAMPLE)
{
if (header->type == PERF_EVENT_SAMPLE)
collector->n_samples++;
process_event (collector, counter, (counter_event_t *)header);
}
tail += header->size;
}
counter->tail = tail;
counter->mmap_page->data_tail = tail;
if (collector->callback)
{
if (collector->n_samples - collector->prev_samples >= N_WAKEUP_EVENTS)
{
gboolean first_sample = collector->prev_samples == 0;
collector->callback (first_sample, collector->data);
collector->prev_samples = collector->n_samples;
}
}
}
/* FIXME: return proper errors */
#define fail(x) \
do { \
perror (x); \
exit (-1); \
} while (0)
static void *
map_buffer (counter_t *counter)
{
int n_bytes = N_PAGES * get_page_size();
void *address, *a;
/* We use the old trick of mapping the ring buffer twice
* consecutively, so that we don't need special-case code
* to deal with wrapping.
*/
address = mmap (NULL, n_bytes * 2 + get_page_size(), PROT_NONE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (address == MAP_FAILED)
fail ("mmap");
a = mmap (address + n_bytes, n_bytes + get_page_size(),
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_FIXED, counter->fd, 0);
if (a != address + n_bytes)
fail ("mmap");
a = mmap (address, n_bytes + get_page_size(),
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_FIXED, counter->fd, 0);
if (a == MAP_FAILED)
fail ("mmap");
if (a != address)
fail ("mmap");
return address;
}
static counter_t *
counter_new (Collector *collector,
int cpu)
{
struct perf_counter_attr attr;
counter_t *counter;
int fd;
counter = g_new (counter_t, 1);
memset (&attr, 0, sizeof (attr));
attr.type = PERF_TYPE_HARDWARE;
attr.config = PERF_COUNT_HW_CPU_CYCLES;
attr.sample_period = 1200000 ; /* In number of clock cycles -
* FIXME: consider using frequency instead
*/
attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_CALLCHAIN;
attr.wakeup_events = N_WAKEUP_EVENTS;
attr.disabled = TRUE;
attr.mmap = 1;
attr.comm = 1;
attr.task = 1;
if ((fd = sysprof_perf_counter_open (&attr, -1, cpu, -1, 0)) < 0)
{
if (errno == ENODEV)
{
attr.type = PERF_TYPE_SOFTWARE;
attr.config = PERF_COUNT_SW_CPU_CLOCK;
attr.sample_period = 10000000;
fd = sysprof_perf_counter_open (&attr, -1, cpu, -1, 0);
}
}
if (fd < 0)
{
fail ("perf_counter_open");
return NULL;
}
counter->collector = collector;
counter->fd = fd;
counter->mmap_page = map_buffer (counter);
if (counter->mmap_page == MAP_FAILED)
{
fail ("mmap");
return NULL;
}
counter->data = (uint8_t *)counter->mmap_page + get_page_size ();
counter->tail = 0;
counter->cpu = cpu;
fd_add_watch (fd, counter);
fd_set_read_callback (fd, on_read);
return counter;
}
static void
counter_enable (counter_t *counter)
{
ioctl (counter->fd, PERF_COUNTER_IOC_ENABLE);
}
static void
counter_disable (counter_t *counter)
{
d_print ("disable\n");
ioctl (counter->fd, PERF_COUNTER_IOC_DISABLE);
}
static void
counter_free (counter_t *counter)
{
d_print ("munmap\n");
munmap (counter->mmap_page, (N_PAGES + 1) * get_page_size());
fd_remove_watch (counter->fd);
close (counter->fd);
g_free (counter);
}
/*
* Collector
*/
static void
enable_counters (Collector *collector)
{
GList *list;
d_print ("enable\n");
for (list = collector->counters; list != NULL; list = list->next)
{
counter_t *counter = list->data;
counter_enable (counter);
}
}
static void
disable_counters (Collector *collector)
{
GList *list;
d_print ("disable\n");
for (list = collector->counters; list != NULL; list = list->next)
{
counter_t *counter = list->data;
counter_disable (counter);
}
}
void
collector_reset (Collector *collector)
{
/* Disable the counters so that we won't track
* the activity of tracker_free()/tracker_new()
*
* They will still record fork/mmap/etc. so
* we can keep an accurate log of process creation
*/
if (collector->counters)
{
d_print ("disable counters\n");
disable_counters (collector);
}
if (collector->tracker)
{
tracker_free (collector->tracker);
collector->tracker = tracker_new ();
}
collector->n_samples = 0;
collector->prev_samples = 0;
g_get_current_time (&collector->latest_reset);
if (collector->counters)
{
d_print ("enable counters\n");
enable_counters (collector);
}
}
/* callback is called whenever a new sample arrives */
Collector *
collector_new (CollectorFunc callback,
gpointer data)
{
Collector *collector = g_new0 (Collector, 1);
collector->callback = callback;
collector->data = data;
collector->tracker = NULL;
collector_reset (collector);
return collector;
}
static void
process_mmap (Collector *collector, mmap_event_t *mmap)
{
tracker_add_map (collector->tracker,
mmap->pid,
mmap->addr,
mmap->addr + mmap->len,
mmap->pgoff,
0, /* inode */
mmap->filename);
}
static void
process_comm (Collector *collector, comm_event_t *comm)
{
d_print ("pid, tid: %d %d", comm->pid, comm->tid);
tracker_add_process (collector->tracker,
comm->pid,
comm->comm);
}
static void
process_fork (Collector *collector, fork_event_t *fork)
{
d_print ("ppid: %d pid: %d ptid: %d tid %d",
fork->ppid, fork->pid, fork->ptid, fork->tid);
tracker_add_fork (collector->tracker, fork->ppid, fork->pid);
}
static void
process_exit (Collector *collector, exit_event_t *exit)
{
d_print ("for %d %d", exit->pid, exit->tid);
tracker_add_exit (collector->tracker, exit->pid);
}
static void
process_sample (Collector *collector,
sample_event_t *sample)
{
uint64_t *ips;
int n_ips;
d_print ("pid, tid: %d %d", sample->pid, sample->tid);
if (sample->n_ips == 0)
{
uint64_t trace[3];
if (sample->header.misc & PERF_EVENT_MISC_KERNEL)
{
trace[0] = PERF_CONTEXT_KERNEL;
trace[1] = sample->ip;
trace[2] = PERF_CONTEXT_USER;
ips = trace;
n_ips = 3;
}
else
{
trace[0] = PERF_CONTEXT_USER;
trace[1] = sample->ip;
ips = trace;
n_ips = 2;
}
}
else
{
ips = sample->ips;
n_ips = sample->n_ips;
}
tracker_add_sample (collector->tracker,
sample->pid, ips, n_ips);
}
static void
process_event (Collector *collector,
counter_t *counter,
counter_event_t *event)
{
char *name;
switch (event->header.type)
{
case PERF_EVENT_MMAP: name = "mmap"; break;
case PERF_EVENT_LOST: name = "lost"; break;
case PERF_EVENT_COMM: name = "comm"; break;
case PERF_EVENT_EXIT: name = "exit"; break;
case PERF_EVENT_THROTTLE: name = "throttle"; break;
case PERF_EVENT_UNTHROTTLE: name = "unthrottle"; break;
case PERF_EVENT_FORK: name = "fork"; break;
case PERF_EVENT_READ: name = "read"; break;
case PERF_EVENT_SAMPLE: name = "samp"; break;
default: name = "unknown"; break;
}
d_print ("cpu %d :: %s :: ", counter->cpu, name);
switch (event->header.type)
{
case PERF_EVENT_MMAP:
process_mmap (collector, &event->mmap);
break;
case PERF_EVENT_LOST:
g_print ("lost event\n");
break;
case PERF_EVENT_COMM:
process_comm (collector, &event->comm);
break;
case PERF_EVENT_EXIT:
process_exit (collector, &event->exit);
break;
case PERF_EVENT_THROTTLE:
g_print ("throttle\n");
break;
case PERF_EVENT_UNTHROTTLE:
g_print ("unthrottle\n");
break;
case PERF_EVENT_FORK:
process_fork (collector, &event->fork);
break;
case PERF_EVENT_READ:
break;
case PERF_EVENT_SAMPLE:
process_sample (collector, &event->sample);
break;
default:
g_warning ("unknown event: %d (%d)\n",
event->header.type, event->header.size);
break;
}
d_print ("\n");
}
gboolean
collector_start (Collector *collector,
GError **err)
{
int n_cpus = get_n_cpus ();
int i;
if (!collector->tracker)
collector->tracker = tracker_new ();
for (i = 0; i < n_cpus; ++i)
{
counter_t *counter = counter_new (collector, i);
collector->counters = g_list_append (collector->counters, counter);
}
enable_counters (collector);
return TRUE;
}
void
collector_stop (Collector *collector)
{
GList *list;
if (!collector->counters)
return;
/* Read any remaining data */
for (list = collector->counters; list != NULL; list = list->next)
{
counter_t *counter = list->data;
on_read (counter);
counter_free (counter);
}
g_list_free (collector->counters);
collector->counters = NULL;
}
int
collector_get_n_samples (Collector *collector)
{
return collector->n_samples;
}
Profile *
collector_create_profile (Collector *collector)
{
/* The collector must be stopped when you create a profile */
g_assert (!collector->counters);
return tracker_create_profile (collector->tracker);
}
GQuark
collector_error_quark (void)
{
static GQuark q = 0;
if (q == 0)
q = g_quark_from_static_string ("collector-error-quark");
return q;
}
|