forked from teawater/kgtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperf_event.c
1144 lines (982 loc) · 27.9 KB
/
perf_event.c
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Following part is base on kernel/events/core.c
* commit 9985c20f9e4aee6857c08246b273a3695a52b929
* Following part is base on kernel/events/internal.h
* commit a8b0ca17b80e92faab46ee7179ba9e99ccb61233
* Following part is base on kernel/events/ring_buffer.c
* commit a7ac67ea021b4603095d2aa458bc41641238f22c
*/
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3,3,0))
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,4))
struct ring_buffer {
atomic_t refcount;
struct rcu_head rcu_head;
#ifdef CONFIG_PERF_USE_VMALLOC
struct work_struct work;
int page_order; /* allocation order */
#endif
int nr_pages; /* nr of data pages */
int writable; /* are we writable */
atomic_t poll; /* POLL_ for wakeups */
local_t head; /* write position */
local_t nest; /* nested writers */
local_t events; /* event limit */
local_t wakeup; /* wakeup stamp */
local_t lost; /* nr records lost */
long watermark; /* wakeup watermark */
struct perf_event_mmap_page *user_page;
void *data_pages[0];
};
#endif
static inline u64 perf_clock(void)
{
return GTP_LOCAL_CLOCK;
}
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,36)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,1))
static inline struct perf_cpu_context *
__get_cpu_context(struct perf_event_context *ctx)
{
return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
}
#else
static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
#endif
/* KGTP doesn't support PERF_FLAG_PID_CGROUP */
static inline int is_cgroup_event(struct perf_event *event)
{
return 0;
}
/* KGTP doesn't support PERF_FLAG_PID_CGROUP */
static inline bool
perf_cgroup_match(struct perf_event *event)
{
return true;
}
/*
* Update the record of the current time in a context.
*/
static void update_context_time(struct perf_event_context *ctx)
{
u64 now = perf_clock();
ctx->time += now - ctx->timestamp;
ctx->timestamp = now;
}
static u64 perf_event_time(struct perf_event *event)
{
struct perf_event_context *ctx = event->ctx;
/* KGTP doesn't support PERF_FLAG_PID_CGROUP */
/*
if (is_cgroup_event(event))
return perf_cgroup_event_time(event);
*/
return ctx ? ctx->time : 0;
}
/*
* Update the total_time_enabled and total_time_running fields for a event.
*/
static void update_event_times(struct perf_event *event)
{
struct perf_event_context *ctx = event->ctx;
u64 run_end;
if (event->state < PERF_EVENT_STATE_INACTIVE ||
event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
return;
/*
* in cgroup mode, time_enabled represents
* the time the event was enabled AND active
* tasks were in the monitored cgroup. This is
* independent of the activity of the context as
* there may be a mix of cgroup and non-cgroup events.
*
* That is why we treat cgroup events differently
* here.
*/
if (is_cgroup_event(event))
run_end = perf_event_time(event);
else if (ctx->is_active)
run_end = ctx->time;
else
run_end = event->tstamp_stopped;
event->total_time_enabled = run_end - event->tstamp_enabled;
if (event->state == PERF_EVENT_STATE_INACTIVE)
run_end = event->tstamp_stopped;
else
run_end = perf_event_time(event);
event->total_time_running = run_end - event->tstamp_running;
}
static inline int
event_filter_match(struct perf_event *event)
{
return (event->cpu == -1 || event->cpu == smp_processor_id())
&& perf_cgroup_match(event);
}
static void
event_sched_out(struct perf_event *event,
struct perf_cpu_context *cpuctx,
struct perf_event_context *ctx)
{
u64 tstamp = perf_event_time(event);
u64 delta;
/*
* An event which could not be activated because of
* filter mismatch still needs to have its timings
* maintained, otherwise bogus information is return
* via read() for time_enabled, time_running:
*/
if (event->state == PERF_EVENT_STATE_INACTIVE
&& !event_filter_match(event)) {
delta = tstamp - event->tstamp_stopped;
event->tstamp_running += delta;
event->tstamp_stopped = tstamp;
}
if (event->state != PERF_EVENT_STATE_ACTIVE)
return;
event->state = PERF_EVENT_STATE_INACTIVE;
if (event->pending_disable) {
event->pending_disable = 0;
event->state = PERF_EVENT_STATE_OFF;
}
event->tstamp_stopped = tstamp;
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,36)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,1))
event->pmu->del(event, 0);
#else
event->pmu->disable(event);
#endif
event->oncpu = -1;
if (!is_software_event(event))
cpuctx->active_oncpu--;
ctx->nr_active--;
if (event->attr.exclusive || !cpuctx->active_oncpu)
cpuctx->exclusive = 0;
}
/*
* Put a event into inactive state and update time fields.
* Enabling the leader of a group effectively enables all
* the group members that aren't explicitly disabled, so we
* have to update their ->tstamp_enabled also.
* Note: this works for group members as well as group leaders
* since the non-leader members' sibling_lists will be empty.
*/
static void __perf_event_mark_enabled(struct perf_event *event,
struct perf_event_context *ctx)
{
struct perf_event *sub;
u64 tstamp = perf_event_time(event);
event->state = PERF_EVENT_STATE_INACTIVE;
event->tstamp_enabled = tstamp - event->total_time_enabled;
list_for_each_entry(sub, &event->sibling_list, group_entry) {
if (sub->state >= PERF_EVENT_STATE_INACTIVE)
sub->tstamp_enabled = tstamp - sub->total_time_enabled;
}
}
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34)) \
&& (RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(6,1))
/*
* Return 1 for a group consisting entirely of software events,
* 0 if the group contains any hardware events.
*/
static int is_software_only_group(struct perf_event *leader)
{
struct perf_event *event;
if (!is_software_event(leader))
return 0;
list_for_each_entry(event, &leader->sibling_list, group_entry)
if (!is_software_event(event))
return 0;
return 1;
}
#endif
/*
* Work out whether we can put this event group on the CPU now.
*/
static int group_can_go_on(struct perf_event *event,
struct perf_cpu_context *cpuctx,
int can_add_hw)
{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34)) \
&& (RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(6,1))
/*
* Groups consisting entirely of software events can always go on.
*/
if (is_software_only_group(event))
return 1;
#else
/*
* Groups consisting entirely of software events can always go on.
*/
if (event->group_flags & PERF_GROUP_SOFTWARE)
return 1;
#endif
/*
* If an exclusive group is already on, no other hardware
* events can go on.
*/
if (cpuctx->exclusive)
return 0;
/*
* If this group is exclusive and there are already
* events on the CPU, it can't go on.
*/
if (event->attr.exclusive && cpuctx->active_oncpu)
return 0;
/*
* Otherwise, try to add it if all previous groups were able
* to go on.
*/
return can_add_hw;
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,4))
#ifndef CONFIG_PERF_USE_VMALLOC
static inline int page_order(struct ring_buffer *rb)
{
return 0;
}
#else
/*
* Back perf_mmap() with vmalloc memory.
*
* Required for architectures that have d-cache aliasing issues.
*/
static inline int page_order(struct ring_buffer *rb)
{
return rb->page_order;
}
#endif
#else
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,1))
#ifndef CONFIG_PERF_USE_VMALLOC
static inline int page_order(struct perf_buffer *buffer)
{
return 0;
}
#else
static inline int page_order(struct perf_buffer *buffer)
{
return buffer->page_order;
}
#endif
#else
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
#ifndef CONFIG_PERF_USE_VMALLOC
static inline int page_order(struct perf_mmap_data *data)
{
return 0;
}
#else
static inline int page_order(struct perf_mmap_data *data)
{
return data->page_order;
}
#endif
#else
static inline int page_order(struct perf_mmap_data *data)
{
return data->data_order;
}
#endif
#endif
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,1))
__always_inline void perf_output_copy(struct perf_output_handle *handle,
const void *buf, unsigned int len)
{
do {
unsigned long size = min_t(unsigned long, handle->size, len);
memcpy(handle->addr, buf, size);
len -= size;
handle->addr += size;
buf += size;
handle->size -= size;
if (!handle->size) {
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,4))
struct ring_buffer *buffer = handle->rb;
#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,1))
struct perf_buffer *buffer = handle->buffer;
#else
struct perf_mmap_data *buffer = handle->data;
#endif
handle->page++;
handle->page &= buffer->nr_pages - 1;
handle->addr = buffer->data_pages[handle->page];
handle->size = PAGE_SIZE << page_order(buffer);
}
} while (len);
}
#else
void perf_output_copy(struct perf_output_handle *handle,
const void *buf, unsigned int len)
{
unsigned int pages_mask;
unsigned long offset;
unsigned int size;
void **pages;
offset = handle->offset;
pages_mask = handle->data->nr_pages - 1;
pages = handle->data->data_pages;
do {
unsigned long page_offset;
unsigned long page_size;
int nr;
nr = (offset >> PAGE_SHIFT) & pages_mask;
page_size = 1UL << (handle->data->data_order + PAGE_SHIFT);
page_offset = offset & (page_size - 1);
size = min_t(unsigned int, page_size - page_offset, len);
memcpy(pages[nr] + page_offset, buf, size);
len -= size;
buf += size;
offset += size;
} while (len);
handle->offset = offset;
/*
* Check we didn't copy past our reservation window, taking the
* possible unsigned int wrap into account.
*/
WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
}
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,38))
/*
* If we inherit events we want to return the parent event id
* to userspace.
*/
static u64 primary_event_id(struct perf_event *event)
{
u64 id = event->id;
if (event->parent)
id = event->parent->id;
return id;
}
static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
{
/*
* only top level events have the pid namespace they were created in
*/
if (event->parent)
event = event->parent;
return task_tgid_nr_ns(p, event->ns);
}
static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
{
/*
* only top level events have the pid namespace they were created in
*/
if (event->parent)
event = event->parent;
return task_pid_nr_ns(p, event->ns);
}
static void __perf_event_header__init_id(struct perf_event_header *header,
struct perf_sample_data *data,
struct perf_event *event)
{
u64 sample_type = event->attr.sample_type;
data->type = sample_type;
header->size += event->id_header_size;
if (sample_type & PERF_SAMPLE_TID) {
/* namespace issues */
data->tid_entry.pid = perf_event_pid(event, current);
data->tid_entry.tid = perf_event_tid(event, current);
}
if (sample_type & PERF_SAMPLE_TIME)
data->time = perf_clock();
if (sample_type & PERF_SAMPLE_ID)
data->id = primary_event_id(event);
if (sample_type & PERF_SAMPLE_STREAM_ID)
data->stream_id = event->id;
if (sample_type & PERF_SAMPLE_CPU) {
data->cpu_entry.cpu = raw_smp_processor_id();
data->cpu_entry.reserved = 0;
}
}
static void __perf_event__output_id_sample(struct perf_output_handle *handle,
struct perf_sample_data *data)
{
u64 sample_type = data->type;
if (sample_type & PERF_SAMPLE_TID)
perf_output_put(handle, data->tid_entry);
if (sample_type & PERF_SAMPLE_TIME)
perf_output_put(handle, data->time);
if (sample_type & PERF_SAMPLE_ID)
perf_output_put(handle, data->id);
if (sample_type & PERF_SAMPLE_STREAM_ID)
perf_output_put(handle, data->stream_id);
if (sample_type & PERF_SAMPLE_CPU)
perf_output_put(handle, data->cpu_entry);
}
static void perf_event_header__init_id(struct perf_event_header *header,
struct perf_sample_data *data,
struct perf_event *event)
{
if (event->attr.sample_id_all)
__perf_event_header__init_id(header, data, event);
}
static void perf_event__output_id_sample(struct perf_event *event,
struct perf_output_handle *handle,
struct perf_sample_data *sample)
{
if (event->attr.sample_id_all)
__perf_event__output_id_sample(handle, sample);
}
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3,1,0)) \
&& (RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(6,4))
static void gtp_perf_event_wakeup(struct perf_event *event)
{
wake_up_all(&event->waitq);
if (event->pending_kill) {
kill_fasync(&event->fasync, SIGIO, event->pending_kill);
event->pending_kill = 0;
}
}
#endif
static void perf_output_wakeup(struct perf_output_handle *handle)
{
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,4))
atomic_set(&handle->rb->poll, POLL_IN);
#else
atomic_set(&handle->buffer->poll, POLL_IN);
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3,1,0)) \
&& (RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(6,4))
if (handle->nmi) {
#endif
handle->event->pending_wakeup = 1;
irq_work_queue(&handle->event->pending);
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3,1,0)) \
&& (RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(6,4))
} else
gtp_perf_event_wakeup(handle->event);
#endif
}
static void perf_output_put_handle(struct perf_output_handle *handle)
{
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,4))
struct ring_buffer *buffer = handle->rb;
#else
struct perf_buffer *buffer = handle->buffer;
#endif
unsigned long head;
again:
head = local_read(&buffer->head);
/*
* IRQ/NMI can happen here, which means we can miss a head update.
*/
if (!local_dec_and_test(&buffer->nest))
goto out;
/*
* Publish the known good head. Rely on the full barrier implied
* by atomic_dec_and_test() order the buffer->head read and this
* write.
*/
buffer->user_page->data_head = head;
/*
* Now check if we missed an update, rely on the (compiler)
* barrier in atomic_dec_and_test() to re-read buffer->head.
*/
if (unlikely(head != local_read(&buffer->head))) {
local_inc(&buffer->nest);
goto again;
}
if (handle->wakeup != local_read(&buffer->wakeup))
perf_output_wakeup(handle);
out:
preempt_enable();
}
static void gtp_perf_output_end(struct perf_output_handle *handle)
{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3,1,0)) \
&& (RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(6,4))
struct perf_event *event = handle->event;
struct perf_buffer *buffer = handle->buffer;
int wakeup_events = event->attr.wakeup_events;
if (handle->sample && wakeup_events) {
int events = local_inc_return(&buffer->events);
if (events >= wakeup_events) {
local_sub(wakeup_events, &buffer->events);
local_inc(&buffer->wakeup);
}
}
#endif
perf_output_put_handle(handle);
rcu_read_unlock();
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,4))
static unsigned long perf_data_size(struct ring_buffer *buffer)
#else
static unsigned long perf_data_size(struct perf_buffer *buffer)
#endif
{
return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
}
/*
* Output
*/
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,4))
static bool perf_output_space(struct ring_buffer *buffer, unsigned long tail,
unsigned long offset, unsigned long head)
#else
static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
unsigned long offset, unsigned long head)
#endif
{
unsigned long mask;
if (!buffer->writable)
return true;
mask = perf_data_size(buffer) - 1;
offset = (offset - tail) & mask;
head = (head - tail) & mask;
if ((int)(head - offset) < 0)
return false;
return true;
}
/*
* We need to ensure a later event_id doesn't publish a head when a former
* event isn't done writing. However since we need to deal with NMIs we
* cannot fully serialize things.
*
* We only publish the head (and generate a wakeup) when the outer-most
* event completes.
*/
static void perf_output_get_handle(struct perf_output_handle *handle)
{
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,4))
struct ring_buffer *buffer = handle->rb;
#else
struct perf_buffer *buffer = handle->buffer;
#endif
preempt_disable();
local_inc(&buffer->nest);
handle->wakeup = local_read(&buffer->wakeup);
}
static int gtp_perf_output_begin(struct perf_output_handle *handle,
struct perf_event *event, unsigned int size,
int nmi, int sample)
{
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,4))
struct ring_buffer *buffer;
#else
struct perf_buffer *buffer;
#endif
unsigned long tail, offset, head;
int have_lost;
struct perf_sample_data sample_data;
struct {
struct perf_event_header header;
u64 id;
u64 lost;
} lost_event;
rcu_read_lock();
/*
* For inherited events we send all the output towards the parent.
*/
if (event->parent)
event = event->parent;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,4))
buffer = rcu_dereference(event->rb);
#else
buffer = rcu_dereference(event->buffer);
#endif
if (!buffer)
goto out;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,4))
handle->rb = buffer;
#else
handle->buffer = buffer;
#endif
handle->event = event;
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3,1,0)) \
&& (RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(6,4))
handle->nmi = nmi;
handle->sample = sample;
#endif
if (!buffer->nr_pages)
goto out;
have_lost = local_read(&buffer->lost);
if (have_lost) {
lost_event.header.size = sizeof(lost_event);
perf_event_header__init_id(&lost_event.header, &sample_data,
event);
size += lost_event.header.size;
}
perf_output_get_handle(handle);
do {
/*
* Userspace could choose to issue a mb() before updating the
* tail pointer. So that all reads will be completed before the
* write is issued.
*/
tail = ACCESS_ONCE(buffer->user_page->data_tail);
smp_rmb();
offset = head = local_read(&buffer->head);
head += size;
if (unlikely(!perf_output_space(buffer, tail, offset, head)))
goto fail;
} while (local_cmpxchg(&buffer->head, offset, head) != offset);
if (head - local_read(&buffer->wakeup) > buffer->watermark)
local_add(buffer->watermark, &buffer->wakeup);
handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
handle->page &= buffer->nr_pages - 1;
handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
handle->addr = buffer->data_pages[handle->page];
handle->addr += handle->size;
handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
if (have_lost) {
lost_event.header.type = PERF_RECORD_LOST;
lost_event.header.misc = 0;
lost_event.id = event->id;
lost_event.lost = local_xchg(&buffer->lost, 0);
perf_output_put(handle, lost_event);
perf_event__output_id_sample(event, handle, &sample_data);
}
return 0;
fail:
local_inc(&buffer->lost);
perf_output_put_handle(handle);
out:
rcu_read_unlock();
return -ENOSPC;
}
/*
* IRQ throttle logging
*/
static void perf_log_throttle(struct perf_event *event, int enable)
{
struct perf_output_handle handle;
struct perf_sample_data sample;
int ret;
struct {
struct perf_event_header header;
u64 time;
u64 id;
u64 stream_id;
} throttle_event = {
.header = {
.type = PERF_RECORD_THROTTLE,
.misc = 0,
.size = sizeof(throttle_event),
},
.time = perf_clock(),
.id = primary_event_id(event),
.stream_id = event->id,
};
if (enable)
throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
perf_event_header__init_id(&throttle_event.header, &sample, event);
ret = gtp_perf_output_begin(&handle, event,
throttle_event.header.size, 1, 0);
if (ret)
return;
perf_output_put(&handle, throttle_event);
perf_event__output_id_sample(event, &handle, &sample);
gtp_perf_output_end(&handle);
}
#endif
static void perf_set_shadow_time(struct perf_event *event,
struct perf_event_context *ctx,
u64 tstamp)
{
/*
* use the correct time source for the time snapshot
*
* We could get by without this by leveraging the
* fact that to get to this function, the caller
* has most likely already called update_context_time()
* and update_cgrp_time_xx() and thus both timestamp
* are identical (or very close). Given that tstamp is,
* already adjusted for cgroup, we could say that:
* tstamp - ctx->timestamp
* is equivalent to
* tstamp - cgrp->timestamp.
*
* Then, in perf_output_read(), the calculation would
* work with no changes because:
* - event is guaranteed scheduled in
* - no scheduled out in between
* - thus the timestamp would be the same
*
* But this is a bit hairy.
*
* So instead, we have an explicit cgroup call to remain
* within the time time source all along. We believe it
* is cleaner and simpler to understand.
*/
/* KGTP doesn't support PERF_FLAG_PID_CGROUP */
#if 0
if (is_cgroup_event(event))
perf_cgroup_set_shadow_time(event, tstamp);
else
#endif
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,36)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,1))
event->shadow_ctx_time = tstamp - ctx->timestamp;
#endif
}
#define MAX_INTERRUPTS (~0ULL)
static int
event_sched_in(struct perf_event *event,
struct perf_cpu_context *cpuctx,
struct perf_event_context *ctx)
{
u64 tstamp = perf_event_time(event);
if (event->state <= PERF_EVENT_STATE_OFF)
return 0;
event->state = PERF_EVENT_STATE_ACTIVE;
event->oncpu = smp_processor_id();
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,38))
/*
* Unthrottle events, since we scheduled we might have missed several
* ticks already, also for a heavily scheduling task there is little
* guarantee it'll get a tick in a timely manner.
*/
if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
perf_log_throttle(event, 1);
event->hw.interrupts = 0;
}
#endif
/*
* The new state must be visible before we turn it on in the hardware:
*/
smp_wmb();
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,36)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,1))
if (event->pmu->add(event, PERF_EF_START)) {
#else
if (event->pmu->enable(event)) {
#endif
event->state = PERF_EVENT_STATE_INACTIVE;
event->oncpu = -1;
return -EAGAIN;
}
event->tstamp_running += tstamp - event->tstamp_stopped;
perf_set_shadow_time(event, ctx, tstamp);
if (!is_software_event(event))
cpuctx->active_oncpu++;
ctx->nr_active++;
if (event->attr.exclusive)
cpuctx->exclusive = 1;
return 0;
}
/*
* Cross CPU call to enable a performance event
*/
static int __gtp_perf_event_enable(void *info)
{
struct perf_event *event = info;
struct perf_event_context *ctx = event->ctx;
struct perf_event *leader = event->group_leader;
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,36)) \
|| (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,1))
struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
#else
struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
#endif
int err;
if (WARN_ON_ONCE(!ctx->is_active))
return -EINVAL;
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,32))
raw_spin_lock(&ctx->lock);
#else
spin_lock(&ctx->lock);
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0))
ctx->is_active = 1;
#endif
update_context_time(ctx);
if (event->state >= PERF_EVENT_STATE_INACTIVE)
goto unlock;
/* KGTP doesn't support PERF_FLAG_PID_CGROUP */
/*
* set current task's cgroup time reference point
*/
/* perf_cgroup_set_timestamp(current, ctx); */
__perf_event_mark_enabled(event, ctx);
if (!event_filter_match(event)) {
/* KGTP doesn't support PERF_FLAG_PID_CGROUP */
/*
if (is_cgroup_event(event))
perf_cgroup_defer_enabled(event);
*/
goto unlock;
}
/*
* If the event is in a group and isn't the group leader,
* then don't put it on unless the group is on.
*/
if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
goto unlock;
if (!group_can_go_on(event, cpuctx, 1)) {
err = -EEXIST;
} else {
/* KGTP doesn't support PERF_FLAG_PID_CGROUP */
/*
if (event == leader)
err = group_sched_in(event, cpuctx, ctx);
else
*/
err = event_sched_in(event, cpuctx, ctx);
}
/* KGTP doesn't support group. */
#if 0
if (err) {
/*
* If this event can't go on and it's part of a
* group, then the whole group has to come off.
*/
if (leader != event)
group_sched_out(leader, cpuctx, ctx);
if (leader->attr.pinned) {
update_group_times(leader);
leader->state = PERF_EVENT_STATE_ERROR;
}
}
#endif
unlock:
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,32))
raw_spin_unlock(&ctx->lock);
#else
spin_unlock(&ctx->lock);
#endif
return 0;
}
/*
* Cross CPU call to disable a performance event
*/