Skip to content

Commit 0cef069

Browse files
committed
refactor: The target real name resolution is now done when needed by the perf module
1 parent dadafe1 commit 0cef069

File tree

2 files changed

+46
-35
lines changed

2 files changed

+46
-35
lines changed

src/perf.c

+45-35
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,15 @@ perf_group_context_destroy(struct perf_group_context **ctx)
148148
}
149149

150150
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)
152152
{
153153
struct perf_context *ctx = malloc(sizeof(struct perf_context));
154154

155155
if (!ctx)
156156
return NULL;
157157

158158
ctx->config = config;
159+
ctx->target_name = target_name;
159160
ctx->terminated = false;
160161
ctx->pipe = pipe;
161162
ctx->ticker = zsock_new_sub("inproc://ticker", "CLOCK_TICK");
@@ -174,7 +175,6 @@ perf_context_destroy(struct perf_context *ctx)
174175
if (!ctx)
175176
return;
176177

177-
perf_config_destroy(ctx->config);
178178
zpoller_destroy(&ctx->poller);
179179
zsock_destroy(&ctx->ticker);
180180
zsock_destroy(&ctx->reporting);
@@ -195,19 +195,19 @@ perf_events_group_setup_cpu(struct perf_context *ctx, struct perf_group_cpu_cont
195195
errno = 0;
196196
cpu = strtol(cpu_id, &cpu_id_endp, 0);
197197
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);
199199
return -1;
200200
}
201201
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);
203203
return -1;
204204
}
205205

206206
for (event = zlistx_first(group->events); event; event = zlistx_next(group->events)) {
207207
errno = 0;
208208
perf_fd = perf_event_open(&event->attr, ctx->cgroup_fd, (int) cpu, group_fd, perf_flags);
209209
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);
211211
return -1;
212212
}
213213

@@ -239,7 +239,7 @@ perf_events_groups_initialize(struct perf_context *ctx)
239239
errno = 0;
240240
ctx->cgroup_fd = open(ctx->config->target->cgroup_path, O_RDONLY);
241241
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);
243243
goto error;
244244
}
245245
}
@@ -250,7 +250,7 @@ perf_events_groups_initialize(struct perf_context *ctx)
250250
/* create group context */
251251
group_ctx = perf_group_context_create(events_group);
252252
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);
254254
goto error;
255255
}
256256

@@ -260,21 +260,21 @@ perf_events_groups_initialize(struct perf_context *ctx)
260260
/* create package context */
261261
pkg_ctx = perf_group_pkg_context_create();
262262
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);
264264
goto error;
265265
}
266266

267267
for (cpu_id = zlistx_first(pkg->cpus_id); cpu_id; cpu_id = zlistx_next(pkg->cpus_id)) {
268268
/* create cpu context */
269269
cpu_ctx = perf_group_cpu_context_create();
270270
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);
272272
goto error;
273273
}
274274

275275
/* open events of the group for the cpu */
276276
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);
278278
goto error;
279279
}
280280

@@ -324,17 +324,17 @@ perf_events_groups_enable(struct perf_context *ctx)
324324
cpu_id = zhashx_cursor(pkg_ctx->cpus_ctx);
325325
group_leader_fd = zlistx_first(cpu_ctx->perf_fds);
326326
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);
328328
continue;
329329
}
330330

331331
errno = 0;
332332
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);
334334

335335
errno = 0;
336336
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);
338338
}
339339
}
340340
}
@@ -366,10 +366,10 @@ handle_pipe(struct perf_context *ctx)
366366

367367
if (streq(command, "$TERM")) {
368368
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);
370370
}
371371
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);
373373

374374
zstr_free(&command);
375375
}
@@ -402,44 +402,44 @@ populate_payload(struct perf_context *ctx, struct payload *payload)
402402
group_name = zhashx_cursor(ctx->groups_ctx);
403403
group_data = payload_group_data_create();
404404
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);
406406
goto error;
407407
}
408408

409409
/* shared perf read buffer */
410410
perf_read_buffer_size = offsetof(struct perf_read_format, values) + sizeof(struct perf_counter_value[zlistx_size(group_ctx->config->events)]);
411411
perf_read_buffer = malloc(perf_read_buffer_size);
412412
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);
414414
goto error;
415415
}
416416

417417
for (pkg_ctx = zhashx_first(group_ctx->pkgs_ctx); pkg_ctx; pkg_ctx = zhashx_next(group_ctx->pkgs_ctx)) {
418418
pkg_id = zhashx_cursor(group_ctx->pkgs_ctx);
419419
pkg_data = payload_pkg_data_create();
420420
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);
422422
goto error;
423423
}
424424

425425
for (cpu_ctx = zhashx_first(pkg_ctx->cpus_ctx); cpu_ctx; cpu_ctx = zhashx_next(pkg_ctx->cpus_ctx)) {
426426
cpu_id = zhashx_cursor(pkg_ctx->cpus_ctx);
427427
cpu_data = payload_cpu_data_create();
428428
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);
430430
goto error;
431431
}
432432

433433
/* read counters value for the cpu */
434434
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);
436436
goto error;
437437
}
438438

439439
/* warn if PMU multiplexing is happening */
440440
perf_multiplexing_ratio = compute_perf_multiplexing_ratio(perf_read_buffer);
441441
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);
443443
}
444444

445445
/* store events value */
@@ -479,14 +479,14 @@ handle_ticker(struct perf_context *ctx)
479479
/* get tick timestamp */
480480
zsock_recv(ctx->ticker, "s8", NULL, &timestamp);
481481

482-
payload = payload_create(timestamp, ctx->config->target->name);
482+
payload = payload_create(timestamp, ctx->target_name);
483483
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);
485485
return;
486486
}
487487

488488
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);
490490
payload_destroy(payload);
491491
return;
492492
}
@@ -498,39 +498,49 @@ handle_ticker(struct perf_context *ctx)
498498
void
499499
perf_monitoring_actor(zsock_t *pipe, void *args)
500500
{
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;
502504
zsock_t *which = NULL;
503505

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;
508512
}
509513

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+
}
511519

512520
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;
516523
}
517524

518525
perf_events_groups_enable(ctx);
519526

520-
zsys_info("perf<%s>: monitoring actor started", ctx->config->target->name);
527+
zsys_info("perf<%s>: monitoring actor started", target_name);
521528

522529
while (!ctx->terminated) {
523530
which = zpoller_wait(ctx->poller, -1);
524531

525532
if (zpoller_terminated(ctx->poller))
526533
break;
527-
534+
528535
if (which == ctx->pipe)
529536
handle_pipe(ctx);
530537
else if (which == ctx->ticker)
531538
handle_ticker(ctx);
532539
}
533540

541+
cleanup:
542+
free(target_name);
543+
perf_config_destroy(config);
534544
perf_context_destroy(ctx);
535545
}
536546

src/perf.h

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct perf_group_context
6363
struct perf_context
6464
{
6565
struct perf_config *config;
66+
const char *target_name;
6667
bool terminated;
6768
zsock_t *pipe;
6869
zsock_t *ticker;

0 commit comments

Comments
 (0)