@@ -148,14 +148,15 @@ perf_group_context_destroy(struct perf_group_context **ctx)
148
148
}
149
149
150
150
static struct perf_context *
151
- perf_context_create (struct perf_config * config , zsock_t * pipe )
151
+ perf_context_create (struct perf_config * config , zsock_t * pipe , const char * target_name )
152
152
{
153
153
struct perf_context * ctx = malloc (sizeof (struct perf_context ));
154
154
155
155
if (!ctx )
156
156
return NULL ;
157
157
158
158
ctx -> config = config ;
159
+ ctx -> target_name = target_name ;
159
160
ctx -> terminated = false;
160
161
ctx -> pipe = pipe ;
161
162
ctx -> ticker = zsock_new_sub ("inproc://ticker" , "CLOCK_TICK" );
@@ -174,7 +175,6 @@ perf_context_destroy(struct perf_context *ctx)
174
175
if (!ctx )
175
176
return ;
176
177
177
- perf_config_destroy (ctx -> config );
178
178
zpoller_destroy (& ctx -> poller );
179
179
zsock_destroy (& ctx -> ticker );
180
180
zsock_destroy (& ctx -> reporting );
@@ -195,19 +195,19 @@ perf_events_group_setup_cpu(struct perf_context *ctx, struct perf_group_cpu_cont
195
195
errno = 0 ;
196
196
cpu = strtol (cpu_id , & cpu_id_endp , 0 );
197
197
if (* cpu_id == '\0' || * cpu_id_endp != '\0' || errno ) {
198
- zsys_error ("perf<%s>: failed convert cpu id for group=%s cpu=%s" , ctx -> config -> target -> name , group -> name , cpu_id );
198
+ zsys_error ("perf<%s>: failed convert cpu id for group=%s cpu=%s" , ctx -> target_name , group -> name , cpu_id );
199
199
return -1 ;
200
200
}
201
201
if (cpu > INT_MAX || cpu < INT_MIN ) {
202
- zsys_error ("perf<%s>: cpu id is out of range for group=%s cpu=%s" , ctx -> config -> target -> name , group -> name , cpu_id );
202
+ zsys_error ("perf<%s>: cpu id is out of range for group=%s cpu=%s" , ctx -> target_name , group -> name , cpu_id );
203
203
return -1 ;
204
204
}
205
205
206
206
for (event = zlistx_first (group -> events ); event ; event = zlistx_next (group -> events )) {
207
207
errno = 0 ;
208
208
perf_fd = perf_event_open (& event -> attr , ctx -> cgroup_fd , (int ) cpu , group_fd , perf_flags );
209
209
if (perf_fd < 1 ) {
210
- zsys_error ("perf<%s>: failed opening perf event for group=%s cpu=%d event=%s errno=%d" , ctx -> config -> target -> name , group -> name , (int ) cpu , event -> name , errno );
210
+ zsys_error ("perf<%s>: failed opening perf event for group=%s cpu=%d event=%s errno=%d" , ctx -> target_name , group -> name , (int ) cpu , event -> name , errno );
211
211
return -1 ;
212
212
}
213
213
@@ -239,7 +239,7 @@ perf_events_groups_initialize(struct perf_context *ctx)
239
239
errno = 0 ;
240
240
ctx -> cgroup_fd = open (ctx -> config -> target -> cgroup_path , O_RDONLY );
241
241
if (ctx -> cgroup_fd < 1 ) {
242
- zsys_error ("perf<%s>: cannot open cgroup dir path=%s errno=%d" , ctx -> config -> target -> name , ctx -> config -> target -> cgroup_path , errno );
242
+ zsys_error ("perf<%s>: cannot open cgroup dir path=%s errno=%d" , ctx -> target_name , ctx -> config -> target -> cgroup_path , errno );
243
243
goto error ;
244
244
}
245
245
}
@@ -250,7 +250,7 @@ perf_events_groups_initialize(struct perf_context *ctx)
250
250
/* create group context */
251
251
group_ctx = perf_group_context_create (events_group );
252
252
if (!group_ctx ) {
253
- zsys_error ("perf<%s>: failed to create context for group=%s" , ctx -> config -> target -> name , events_group_name );
253
+ zsys_error ("perf<%s>: failed to create context for group=%s" , ctx -> target_name , events_group_name );
254
254
goto error ;
255
255
}
256
256
@@ -260,21 +260,21 @@ perf_events_groups_initialize(struct perf_context *ctx)
260
260
/* create package context */
261
261
pkg_ctx = perf_group_pkg_context_create ();
262
262
if (!pkg_ctx ) {
263
- zsys_error ("perf<%s>: failed to create pkg context for group=%s pkg=%s" , ctx -> config -> target -> name , events_group_name , pkg_id );
263
+ zsys_error ("perf<%s>: failed to create pkg context for group=%s pkg=%s" , ctx -> target_name , events_group_name , pkg_id );
264
264
goto error ;
265
265
}
266
266
267
267
for (cpu_id = zlistx_first (pkg -> cpus_id ); cpu_id ; cpu_id = zlistx_next (pkg -> cpus_id )) {
268
268
/* create cpu context */
269
269
cpu_ctx = perf_group_cpu_context_create ();
270
270
if (!cpu_ctx ) {
271
- zsys_error ("perf<%s>: failed to create cpu context for group=%s pkg=%s cpu=%s" , ctx -> config -> target -> name , events_group_name , pkg_id , cpu_id );
271
+ zsys_error ("perf<%s>: failed to create cpu context for group=%s pkg=%s cpu=%s" , ctx -> target_name , events_group_name , pkg_id , cpu_id );
272
272
goto error ;
273
273
}
274
274
275
275
/* open events of the group for the cpu */
276
276
if (perf_events_group_setup_cpu (ctx , cpu_ctx , events_group , perf_flags , cpu_id )) {
277
- zsys_error ("perf<%s>: failed to setup perf for group=%s pkg=%s cpu=%s" , ctx -> config -> target -> name , events_group_name , pkg_id , cpu_id );
277
+ zsys_error ("perf<%s>: failed to setup perf for group=%s pkg=%s cpu=%s" , ctx -> target_name , events_group_name , pkg_id , cpu_id );
278
278
goto error ;
279
279
}
280
280
@@ -324,17 +324,17 @@ perf_events_groups_enable(struct perf_context *ctx)
324
324
cpu_id = zhashx_cursor (pkg_ctx -> cpus_ctx );
325
325
group_leader_fd = zlistx_first (cpu_ctx -> perf_fds );
326
326
if (!group_leader_fd ) {
327
- zsys_error ("perf<%s>: no group leader fd for group=%s pkg=%s cpu=%s" , ctx -> config -> target -> name , group_name , pkg_id , cpu_id );
327
+ zsys_error ("perf<%s>: no group leader fd for group=%s pkg=%s cpu=%s" , ctx -> target_name , group_name , pkg_id , cpu_id );
328
328
continue ;
329
329
}
330
330
331
331
errno = 0 ;
332
332
if (ioctl (* group_leader_fd , PERF_EVENT_IOC_RESET , PERF_IOC_FLAG_GROUP ))
333
- zsys_error ("perf<%s>: cannot reset events for group=%s pkg=%s cpu=%s errno=%d" , ctx -> config -> target -> name , group_name , pkg_id , cpu_id , errno );
333
+ zsys_error ("perf<%s>: cannot reset events for group=%s pkg=%s cpu=%s errno=%d" , ctx -> target_name , group_name , pkg_id , cpu_id , errno );
334
334
335
335
errno = 0 ;
336
336
if (ioctl (* group_leader_fd , PERF_EVENT_IOC_ENABLE , PERF_IOC_FLAG_GROUP ))
337
- zsys_error ("perf<%s>: cannot enable events for group=%s pkg=%s cpu=%s errno=%d" , ctx -> config -> target -> name , group_name , pkg_id , cpu_id , errno );
337
+ zsys_error ("perf<%s>: cannot enable events for group=%s pkg=%s cpu=%s errno=%d" , ctx -> target_name , group_name , pkg_id , cpu_id , errno );
338
338
}
339
339
}
340
340
}
@@ -366,10 +366,10 @@ handle_pipe(struct perf_context *ctx)
366
366
367
367
if (streq (command , "$TERM" )) {
368
368
ctx -> terminated = true;
369
- zsys_info ("perf<%s>: shutting down actor" , ctx -> config -> target -> name );
369
+ zsys_info ("perf<%s>: shutting down actor" , ctx -> target_name );
370
370
}
371
371
else
372
- zsys_error ("perf<%s>: invalid pipe command: %s" , ctx -> config -> target -> name , command );
372
+ zsys_error ("perf<%s>: invalid pipe command: %s" , ctx -> target_name , command );
373
373
374
374
zstr_free (& command );
375
375
}
@@ -402,44 +402,44 @@ populate_payload(struct perf_context *ctx, struct payload *payload)
402
402
group_name = zhashx_cursor (ctx -> groups_ctx );
403
403
group_data = payload_group_data_create ();
404
404
if (!group_data ) {
405
- zsys_error ("perf<%s>: failed to allocate group data for group=%s" , ctx -> config -> target -> name , group_name );
405
+ zsys_error ("perf<%s>: failed to allocate group data for group=%s" , ctx -> target_name , group_name );
406
406
goto error ;
407
407
}
408
408
409
409
/* shared perf read buffer */
410
410
perf_read_buffer_size = offsetof(struct perf_read_format , values ) + sizeof (struct perf_counter_value [zlistx_size (group_ctx -> config -> events )]);
411
411
perf_read_buffer = malloc (perf_read_buffer_size );
412
412
if (!perf_read_buffer ) {
413
- zsys_error ("perf<%s>: failed to allocate perf read buffer for group=%s" , ctx -> config -> target -> name , group_name );
413
+ zsys_error ("perf<%s>: failed to allocate perf read buffer for group=%s" , ctx -> target_name , group_name );
414
414
goto error ;
415
415
}
416
416
417
417
for (pkg_ctx = zhashx_first (group_ctx -> pkgs_ctx ); pkg_ctx ; pkg_ctx = zhashx_next (group_ctx -> pkgs_ctx )) {
418
418
pkg_id = zhashx_cursor (group_ctx -> pkgs_ctx );
419
419
pkg_data = payload_pkg_data_create ();
420
420
if (!pkg_data ) {
421
- zsys_error ("perf<%s>: failed to allocate pkg data for group=%s pkg=%s" , ctx -> config -> target -> name , group_name , pkg_id );
421
+ zsys_error ("perf<%s>: failed to allocate pkg data for group=%s pkg=%s" , ctx -> target_name , group_name , pkg_id );
422
422
goto error ;
423
423
}
424
424
425
425
for (cpu_ctx = zhashx_first (pkg_ctx -> cpus_ctx ); cpu_ctx ; cpu_ctx = zhashx_next (pkg_ctx -> cpus_ctx )) {
426
426
cpu_id = zhashx_cursor (pkg_ctx -> cpus_ctx );
427
427
cpu_data = payload_cpu_data_create ();
428
428
if (!cpu_data ) {
429
- zsys_error ("perf<%s>: failed to allocate cpu data for group=%s pkg=%s cpu=%s" , ctx -> config -> target -> name , group_name , pkg_id , cpu_id );
429
+ zsys_error ("perf<%s>: failed to allocate cpu data for group=%s pkg=%s cpu=%s" , ctx -> target_name , group_name , pkg_id , cpu_id );
430
430
goto error ;
431
431
}
432
432
433
433
/* read counters value for the cpu */
434
434
if (perf_events_group_read_cpu (cpu_ctx , perf_read_buffer , perf_read_buffer_size )) {
435
- zsys_error ("perf<%s>: cannot read perf values for group=%s pkg=%s cpu=%s" , ctx -> config -> target -> name , group_name , pkg_id , cpu_id );
435
+ zsys_error ("perf<%s>: cannot read perf values for group=%s pkg=%s cpu=%s" , ctx -> target_name , group_name , pkg_id , cpu_id );
436
436
goto error ;
437
437
}
438
438
439
439
/* warn if PMU multiplexing is happening */
440
440
perf_multiplexing_ratio = compute_perf_multiplexing_ratio (perf_read_buffer );
441
441
if (perf_multiplexing_ratio < 1.0 ) {
442
- zsys_warning ("perf<%s>: perf multiplexing for group=%s pkg=%s cpu=%s ratio=%f" , ctx -> config -> target -> name , group_name , pkg_id , cpu_id , perf_multiplexing_ratio );
442
+ zsys_warning ("perf<%s>: perf multiplexing for group=%s pkg=%s cpu=%s ratio=%f" , ctx -> target_name , group_name , pkg_id , cpu_id , perf_multiplexing_ratio );
443
443
}
444
444
445
445
/* store events value */
@@ -479,14 +479,14 @@ handle_ticker(struct perf_context *ctx)
479
479
/* get tick timestamp */
480
480
zsock_recv (ctx -> ticker , "s8" , NULL , & timestamp );
481
481
482
- payload = payload_create (timestamp , ctx -> config -> target -> name );
482
+ payload = payload_create (timestamp , ctx -> target_name );
483
483
if (!payload ) {
484
- zsys_error ("perf<%s>: failed to allocate payload for timestamp=%lu" , ctx -> config -> target -> name , timestamp );
484
+ zsys_error ("perf<%s>: failed to allocate payload for timestamp=%lu" , ctx -> target_name , timestamp );
485
485
return ;
486
486
}
487
487
488
488
if (populate_payload (ctx , payload )) {
489
- zsys_error ("perf<%s>: failed to populate payload for timestamp=%lu" , ctx -> config -> target -> name , timestamp );
489
+ zsys_error ("perf<%s>: failed to populate payload for timestamp=%lu" , ctx -> target_name , timestamp );
490
490
payload_destroy (payload );
491
491
return ;
492
492
}
@@ -498,39 +498,49 @@ handle_ticker(struct perf_context *ctx)
498
498
void
499
499
perf_monitoring_actor (zsock_t * pipe , void * args )
500
500
{
501
- struct perf_context * ctx = perf_context_create (args , pipe );
501
+ struct perf_config * config = args ;
502
+ char * target_name = NULL ;
503
+ struct perf_context * ctx = NULL ;
502
504
zsock_t * which = NULL ;
503
505
504
- if (!ctx ) {
505
- zsys_error ("perf<%s>: cannot create perf context" , ((struct perf_config * ) args )-> target -> name );
506
- perf_config_destroy (args );
507
- return ;
506
+ zsock_signal (pipe , 0 );
507
+
508
+ target_name = target_resolve_real_name (config -> target );
509
+ if (!target_name ) {
510
+ zsys_error ("perf: failed to resolve name of target for cgroup '%s'" , config -> target -> cgroup_path );
511
+ goto cleanup ;
508
512
}
509
513
510
- zsock_signal (pipe , 0 );
514
+ ctx = perf_context_create (args , pipe , target_name );
515
+ if (!ctx ) {
516
+ zsys_error ("perf<%s>: cannot create perf context" , target_name );
517
+ goto cleanup ;
518
+ }
511
519
512
520
if (perf_events_groups_initialize (ctx )) {
513
- zsys_error ("perf<%s>: cannot initialize perf monitoring" , ctx -> config -> target -> name );
514
- perf_context_destroy (ctx );
515
- return ;
521
+ zsys_error ("perf<%s>: cannot initialize perf monitoring" , target_name );
522
+ goto cleanup ;
516
523
}
517
524
518
525
perf_events_groups_enable (ctx );
519
526
520
- zsys_info ("perf<%s>: monitoring actor started" , ctx -> config -> target -> name );
527
+ zsys_info ("perf<%s>: monitoring actor started" , target_name );
521
528
522
529
while (!ctx -> terminated ) {
523
530
which = zpoller_wait (ctx -> poller , -1 );
524
531
525
532
if (zpoller_terminated (ctx -> poller ))
526
533
break ;
527
-
534
+
528
535
if (which == ctx -> pipe )
529
536
handle_pipe (ctx );
530
537
else if (which == ctx -> ticker )
531
538
handle_ticker (ctx );
532
539
}
533
540
541
+ cleanup :
542
+ free (target_name );
543
+ perf_config_destroy (config );
534
544
perf_context_destroy (ctx );
535
545
}
536
546
0 commit comments