@cryptotaxi247 / netdata-1 / commits / 1cce860ab

fix(perf.plugin): disable if all events disabled during init (#18728)

Ilya Mashchenko committed Oct 9, 2024 at 10:38 UTC 1cce860ab01fab3ed8d7475d404c2a3f0973ceea
1 file changed +30 -13
src/collectors/perf.plugin/perf_plugin.c
+30 -13
@@ -240,7 +240,7 @@ static struct perf_event {
240 {EV_ID_END, 0, 0, NULL, NULL, 0, 0, 0, NULL, NULL, NULL}
241 };
242
243 -static int perf_init() {
243 +static bool perf_init() {
244 int cpu, group;
245 struct perf_event_attr perf_event_attr;
246 struct perf_event *current_event = NULL;
@@ -270,6 +270,8 @@ static int perf_init() {
270
271 memset(&perf_event_attr, 0, sizeof(perf_event_attr));
272
273 + int enabled = 0;
274 +
275 for(cpu = 0; cpu < number_of_cpus; cpu++) {
276 for(current_event = &perf_events[0]; current_event->id != EV_ID_END; current_event++) {
277 if(unlikely(current_event->disabled)) continue;
@@ -304,6 +306,8 @@ static int perf_init() {
306 }
307 collector_error("Disabling event %u", current_event->id);
308 current_event->disabled = 1;
309 + } else {
310 + enabled++;
311 }
312
313 *(current_event->fd + cpu) = fd;
@@ -313,7 +317,7 @@ static int perf_init() {
317 }
318 }
319
316 - return 0;
320 + return enabled > 0;
321 }
322
323 static void perf_free(void) {
@@ -1295,8 +1299,16 @@ int main(int argc, char **argv) {
1299 else if(freq)
1300 collector_error("update frequency %d seconds is too small for PERF. Using %d.", freq, update_every);
1301
1298 - if(unlikely(debug)) fprintf(stderr, "perf.plugin: calling perf_init()\n");
1299 - int perf = !perf_init();
1302 + if (unlikely(debug))
1303 + fprintf(stderr, "perf.plugin: calling perf_init()\n");
1304 +
1305 + if (!perf_init()) {
1306 + perf_free();
1307 + collector_info("all perf counters are disabled");
1308 + fprintf(stdout, "EXIT\n");
1309 + fflush(stdout);
1310 + exit(1);
1311 + }
1312
1313 // ------------------------------------------------------------------------
1314 // the main loop
@@ -1308,25 +1320,29 @@ int main(int argc, char **argv) {
1320 size_t iteration;
1321 usec_t step = update_every * USEC_PER_SEC;
1322
1323 + int perf = 1;
1324 +
1325 heartbeat_t hb;
1326 heartbeat_init(&hb);
1327 for(iteration = 0; 1; iteration++) {
1328 usec_t dt = heartbeat_next(&hb, step);
1329
1316 - if(unlikely(netdata_exit)) break;
1330 + if (unlikely(netdata_exit))
1331 + break;
1332
1318 - if(unlikely(debug && iteration))
1319 - fprintf(stderr, "perf.plugin: iteration %zu, dt %"PRIu64" usec\n"
1320 - , iteration
1321 - , dt
1322 - );
1333 + if (unlikely(debug && iteration))
1334 + fprintf(stderr, "perf.plugin: iteration %zu, dt %" PRIu64 " usec\n", iteration, dt);
1335
1336 if(likely(perf)) {
1325 - if(unlikely(debug)) fprintf(stderr, "perf.plugin: calling perf_collect()\n");
1337 + if (unlikely(debug))
1338 + fprintf(stderr, "perf.plugin: calling perf_collect()\n");
1339 +
1340 perf = !perf_collect();
1341
1342 if(likely(perf)) {
1329 - if(unlikely(debug)) fprintf(stderr, "perf.plugin: calling perf_send_metrics()\n");
1343 + if (unlikely(debug))
1344 + fprintf(stderr, "perf.plugin: calling perf_send_metrics()\n");
1345 +
1346 perf_send_metrics();
1347 }
1348 }
@@ -1334,7 +1350,8 @@ int main(int argc, char **argv) {
1350 fflush(stdout);
1351
1352 // restart check (14400 seconds)
1337 - if(now_monotonic_sec() - started_t > 14400) break;
1353 + if (now_monotonic_sec() - started_t > 14400)
1354 + break;
1355 }
1356
1357 collector_info("process exiting");