Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf hists: Introduce hist_entry__init function

Move the 'struct hist_entry' initialization code to a separate function.
It'll be useful and more clear for the following patches that introduce
allocation callbacks.

Releasing the hist_entry object in hist_entry__new function
(where it's allocated) rather than in hist_entry__init.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1467701765-26194-2-git-send-email-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Jiri Olsa and committed by
Arnaldo Carvalho de Melo
0a269a6b 44530d58

+77 -70
+77 -70
tools/perf/util/hist.c
··· 352 352 * histogram, sorted on item, collects periods 353 353 */ 354 354 355 + static int hist_entry__init(struct hist_entry *he, 356 + struct hist_entry *template, 357 + bool sample_self) 358 + { 359 + *he = *template; 360 + 361 + if (symbol_conf.cumulate_callchain) { 362 + he->stat_acc = malloc(sizeof(he->stat)); 363 + if (he->stat_acc == NULL) 364 + return -ENOMEM; 365 + memcpy(he->stat_acc, &he->stat, sizeof(he->stat)); 366 + if (!sample_self) 367 + memset(&he->stat, 0, sizeof(he->stat)); 368 + } 369 + 370 + map__get(he->ms.map); 371 + 372 + if (he->branch_info) { 373 + /* 374 + * This branch info is (a part of) allocated from 375 + * sample__resolve_bstack() and will be freed after 376 + * adding new entries. So we need to save a copy. 377 + */ 378 + he->branch_info = malloc(sizeof(*he->branch_info)); 379 + if (he->branch_info == NULL) { 380 + map__zput(he->ms.map); 381 + free(he->stat_acc); 382 + return -ENOMEM; 383 + } 384 + 385 + memcpy(he->branch_info, template->branch_info, 386 + sizeof(*he->branch_info)); 387 + 388 + map__get(he->branch_info->from.map); 389 + map__get(he->branch_info->to.map); 390 + } 391 + 392 + if (he->mem_info) { 393 + map__get(he->mem_info->iaddr.map); 394 + map__get(he->mem_info->daddr.map); 395 + } 396 + 397 + if (symbol_conf.use_callchain) 398 + callchain_init(he->callchain); 399 + 400 + if (he->raw_data) { 401 + he->raw_data = memdup(he->raw_data, he->raw_size); 402 + 403 + if (he->raw_data == NULL) { 404 + map__put(he->ms.map); 405 + if (he->branch_info) { 406 + map__put(he->branch_info->from.map); 407 + map__put(he->branch_info->to.map); 408 + free(he->branch_info); 409 + } 410 + if (he->mem_info) { 411 + map__put(he->mem_info->iaddr.map); 412 + map__put(he->mem_info->daddr.map); 413 + } 414 + free(he->stat_acc); 415 + return -ENOMEM; 416 + } 417 + } 418 + INIT_LIST_HEAD(&he->pairs.node); 419 + thread__get(he->thread); 420 + 421 + if (!symbol_conf.report_hierarchy) 422 + he->leaf = true; 423 + 424 + return 0; 425 + } 426 + 355 427 static struct hist_entry *hist_entry__new(struct hist_entry *template, 356 428 bool sample_self) 357 429 { 358 430 size_t callchain_size = 0; 359 431 struct hist_entry *he; 432 + int err = 0; 360 433 361 434 if (symbol_conf.use_callchain) 362 435 callchain_size = sizeof(struct callchain_root); 363 436 364 437 he = zalloc(sizeof(*he) + callchain_size); 365 - 366 - if (he != NULL) { 367 - *he = *template; 368 - 369 - if (symbol_conf.cumulate_callchain) { 370 - he->stat_acc = malloc(sizeof(he->stat)); 371 - if (he->stat_acc == NULL) { 372 - free(he); 373 - return NULL; 374 - } 375 - memcpy(he->stat_acc, &he->stat, sizeof(he->stat)); 376 - if (!sample_self) 377 - memset(&he->stat, 0, sizeof(he->stat)); 378 - } 379 - 380 - map__get(he->ms.map); 381 - 382 - if (he->branch_info) { 383 - /* 384 - * This branch info is (a part of) allocated from 385 - * sample__resolve_bstack() and will be freed after 386 - * adding new entries. So we need to save a copy. 387 - */ 388 - he->branch_info = malloc(sizeof(*he->branch_info)); 389 - if (he->branch_info == NULL) { 390 - map__zput(he->ms.map); 391 - free(he->stat_acc); 392 - free(he); 393 - return NULL; 394 - } 395 - 396 - memcpy(he->branch_info, template->branch_info, 397 - sizeof(*he->branch_info)); 398 - 399 - map__get(he->branch_info->from.map); 400 - map__get(he->branch_info->to.map); 401 - } 402 - 403 - if (he->mem_info) { 404 - map__get(he->mem_info->iaddr.map); 405 - map__get(he->mem_info->daddr.map); 406 - } 407 - 408 - if (symbol_conf.use_callchain) 409 - callchain_init(he->callchain); 410 - 411 - if (he->raw_data) { 412 - he->raw_data = memdup(he->raw_data, he->raw_size); 413 - 414 - if (he->raw_data == NULL) { 415 - map__put(he->ms.map); 416 - if (he->branch_info) { 417 - map__put(he->branch_info->from.map); 418 - map__put(he->branch_info->to.map); 419 - free(he->branch_info); 420 - } 421 - if (he->mem_info) { 422 - map__put(he->mem_info->iaddr.map); 423 - map__put(he->mem_info->daddr.map); 424 - } 425 - free(he->stat_acc); 426 - free(he); 427 - return NULL; 428 - } 429 - } 430 - INIT_LIST_HEAD(&he->pairs.node); 431 - thread__get(he->thread); 432 - 433 - if (!symbol_conf.report_hierarchy) 434 - he->leaf = true; 438 + if (he) { 439 + err = hist_entry__init(he, template, sample_self); 440 + if (err) 441 + zfree(&he); 435 442 } 436 443 437 444 return he;