1 /*- 2 * Copyright (c) 2005-2007, Joseph Koshy 3 * Copyright (c) 2007 The FreeBSD Foundation 4 * Copyright (c) 2009, Fabien Thomas 5 * All rights reserved. 6 * 7 * Portions of this software were developed by A. Joseph Koshy under 8 * sponsorship from the FreeBSD Foundation and Google, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Transform a hwpmc(4) log into human readable form, and into 34 * gprof(1) compatible profiles. 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include <sys/param.h> 41 #include <sys/endian.h> 42 #include <sys/gmon.h> 43 #include <sys/imgact_aout.h> 44 #include <sys/imgact_elf.h> 45 #include <sys/mman.h> 46 #include <sys/pmc.h> 47 #include <sys/queue.h> 48 #include <sys/socket.h> 49 #include <sys/stat.h> 50 #include <sys/wait.h> 51 52 #include <netinet/in.h> 53 54 #include <assert.h> 55 #include <curses.h> 56 #include <err.h> 57 #include <errno.h> 58 #include <fcntl.h> 59 #include <gelf.h> 60 #include <libgen.h> 61 #include <limits.h> 62 #include <netdb.h> 63 #include <pmc.h> 64 #include <pmclog.h> 65 #include <sysexits.h> 66 #include <stdint.h> 67 #include <stdio.h> 68 #include <stdlib.h> 69 #include <string.h> 70 #include <unistd.h> 71 72 #include "pmcstat.h" 73 #include "pmcstat_log.h" 74 #include "pmcpl_callgraph.h" 75 #include "pmcpl_gprof.h" 76 77 /* 78 * struct pmcstat_gmonfile tracks a given 'gmon.out' file. These 79 * files are mmap()'ed in as needed. 80 */ 81 82 struct pmcstat_gmonfile { 83 LIST_ENTRY(pmcstat_gmonfile) pgf_next; /* list of entries */ 84 int pgf_overflow; /* whether a count overflowed */ 85 pmc_id_t pgf_pmcid; /* id of the associated pmc */ 86 size_t pgf_nbuckets; /* #buckets in this gmon.out */ 87 unsigned int pgf_nsamples; /* #samples in this gmon.out */ 88 pmcstat_interned_string pgf_name; /* pathname of gmon.out file */ 89 size_t pgf_ndatabytes; /* number of bytes mapped */ 90 void *pgf_gmondata; /* pointer to mmap'ed data */ 91 FILE *pgf_file; /* used when writing gmon arcs */ 92 }; 93 94 /* 95 * Prototypes 96 */ 97 98 static void pmcstat_gmon_create_file(struct pmcstat_gmonfile *_pgf, 99 struct pmcstat_image *_image); 100 static pmcstat_interned_string pmcstat_gmon_create_name(const char *_sd, 101 struct pmcstat_image *_img, pmc_id_t _pmcid); 102 static void pmcstat_gmon_map_file(struct pmcstat_gmonfile *_pgf); 103 static void pmcstat_gmon_unmap_file(struct pmcstat_gmonfile *_pgf); 104 105 static struct pmcstat_gmonfile *pmcstat_image_find_gmonfile(struct 106 pmcstat_image *_i, pmc_id_t _id); 107 108 /* 109 * Create a gmon.out file and size it. 110 */ 111 112 static void 113 pmcstat_gmon_create_file(struct pmcstat_gmonfile *pgf, 114 struct pmcstat_image *image) 115 { 116 int fd; 117 size_t count; 118 struct gmonhdr gm; 119 const char *pathname; 120 char buffer[DEFAULT_BUFFER_SIZE]; 121 122 pathname = pmcstat_string_unintern(pgf->pgf_name); 123 if ((fd = open(pathname, O_RDWR|O_NOFOLLOW|O_CREAT, 124 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) 125 err(EX_OSERR, "ERROR: Cannot open \"%s\"", pathname); 126 127 gm.lpc = image->pi_start; 128 gm.hpc = image->pi_end; 129 gm.ncnt = (pgf->pgf_nbuckets * sizeof(HISTCOUNTER)) + 130 sizeof(struct gmonhdr); 131 gm.version = GMONVERSION; 132 gm.profrate = 0; /* use ticks */ 133 gm.histcounter_type = 0; /* compatibility with moncontrol() */ 134 gm.spare[0] = gm.spare[1] = 0; 135 136 /* Write out the gmon header */ 137 if (write(fd, &gm, sizeof(gm)) < 0) 138 goto error; 139 140 /* Zero fill the samples[] array */ 141 (void) memset(buffer, 0, sizeof(buffer)); 142 143 count = pgf->pgf_ndatabytes - sizeof(struct gmonhdr); 144 while (count > sizeof(buffer)) { 145 if (write(fd, &buffer, sizeof(buffer)) < 0) 146 goto error; 147 count -= sizeof(buffer); 148 } 149 150 if (write(fd, &buffer, count) < 0) 151 goto error; 152 153 (void) close(fd); 154 155 return; 156 157 error: 158 err(EX_OSERR, "ERROR: Cannot write \"%s\"", pathname); 159 } 160 161 /* 162 * Determine the full pathname of a gmon.out file for a given 163 * (image,pmcid) combination. Return the interned string. 164 */ 165 166 pmcstat_interned_string 167 pmcstat_gmon_create_name(const char *samplesdir, struct pmcstat_image *image, 168 pmc_id_t pmcid) 169 { 170 const char *pmcname; 171 char fullpath[PATH_MAX]; 172 173 pmcname = pmcstat_pmcid_to_name(pmcid); 174 if (!pmcname) 175 err(EX_SOFTWARE, "ERROR: cannot find pmcid"); 176 177 (void) snprintf(fullpath, sizeof(fullpath), 178 "%s/%s/%s", samplesdir, pmcname, 179 pmcstat_string_unintern(image->pi_samplename)); 180 181 return (pmcstat_string_intern(fullpath)); 182 } 183 184 185 /* 186 * Mmap in a gmon.out file for processing. 187 */ 188 189 static void 190 pmcstat_gmon_map_file(struct pmcstat_gmonfile *pgf) 191 { 192 int fd; 193 const char *pathname; 194 195 pathname = pmcstat_string_unintern(pgf->pgf_name); 196 197 /* the gmon.out file must already exist */ 198 if ((fd = open(pathname, O_RDWR | O_NOFOLLOW, 0)) < 0) 199 err(EX_OSERR, "ERROR: cannot open \"%s\"", pathname); 200 201 pgf->pgf_gmondata = mmap(NULL, pgf->pgf_ndatabytes, 202 PROT_READ|PROT_WRITE, MAP_NOSYNC|MAP_SHARED, fd, 0); 203 204 if (pgf->pgf_gmondata == MAP_FAILED) 205 err(EX_OSERR, "ERROR: cannot map \"%s\"", pathname); 206 207 (void) close(fd); 208 } 209 210 /* 211 * Unmap a gmon.out file after sync'ing its data to disk. 212 */ 213 214 static void 215 pmcstat_gmon_unmap_file(struct pmcstat_gmonfile *pgf) 216 { 217 (void) msync(pgf->pgf_gmondata, pgf->pgf_ndatabytes, 218 MS_SYNC); 219 (void) munmap(pgf->pgf_gmondata, pgf->pgf_ndatabytes); 220 pgf->pgf_gmondata = NULL; 221 } 222 223 static void 224 pmcstat_gmon_append_arc(struct pmcstat_image *image, pmc_id_t pmcid, 225 uintptr_t rawfrom, uintptr_t rawto, uint32_t count) 226 { 227 struct rawarc arc; /* from <sys/gmon.h> */ 228 const char *pathname; 229 struct pmcstat_gmonfile *pgf; 230 231 if ((pgf = pmcstat_image_find_gmonfile(image, pmcid)) == NULL) 232 return; 233 234 if (pgf->pgf_file == NULL) { 235 pathname = pmcstat_string_unintern(pgf->pgf_name); 236 if ((pgf->pgf_file = fopen(pathname, "a")) == NULL) 237 return; 238 } 239 240 arc.raw_frompc = rawfrom + image->pi_vaddr; 241 arc.raw_selfpc = rawto + image->pi_vaddr; 242 arc.raw_count = count; 243 244 (void) fwrite(&arc, sizeof(arc), 1, pgf->pgf_file); 245 246 } 247 248 static struct pmcstat_gmonfile * 249 pmcstat_image_find_gmonfile(struct pmcstat_image *image, pmc_id_t pmcid) 250 { 251 struct pmcstat_gmonfile *pgf; 252 LIST_FOREACH(pgf, &image->pi_gmlist, pgf_next) 253 if (pgf->pgf_pmcid == pmcid) 254 return (pgf); 255 return (NULL); 256 } 257 258 static void 259 pmcstat_cgnode_do_gmon_arcs(struct pmcstat_cgnode *cg, pmc_id_t pmcid) 260 { 261 struct pmcstat_cgnode *cgc; 262 263 /* 264 * Look for child nodes that belong to the same image. 265 */ 266 267 LIST_FOREACH(cgc, &cg->pcg_children, pcg_sibling) { 268 if (cgc->pcg_image == cg->pcg_image) 269 pmcstat_gmon_append_arc(cg->pcg_image, pmcid, 270 cgc->pcg_func, cg->pcg_func, cgc->pcg_count); 271 if (cgc->pcg_nchildren > 0) 272 pmcstat_cgnode_do_gmon_arcs(cgc, pmcid); 273 } 274 } 275 276 static void 277 pmcstat_callgraph_do_gmon_arcs_for_pmcid(pmc_id_t pmcid) 278 { 279 int n; 280 struct pmcstat_cgnode_hash *pch; 281 282 for (n = 0; n < PMCSTAT_NHASH; n++) 283 LIST_FOREACH(pch, &pmcstat_cgnode_hash[n], pch_next) 284 if (pch->pch_pmcid == pmcid && 285 pch->pch_cgnode->pcg_nchildren > 1) 286 pmcstat_cgnode_do_gmon_arcs(pch->pch_cgnode, 287 pmcid); 288 } 289 290 291 static void 292 pmcstat_callgraph_do_gmon_arcs(void) 293 { 294 struct pmcstat_pmcrecord *pmcr; 295 296 LIST_FOREACH(pmcr, &pmcstat_pmcs, pr_next) 297 pmcstat_callgraph_do_gmon_arcs_for_pmcid(pmcr->pr_pmcid); 298 } 299 300 void 301 pmcpl_gmon_initimage(struct pmcstat_image *pi) 302 { 303 int count, nlen; 304 char *sn; 305 char name[NAME_MAX]; 306 307 /* 308 * Look for a suitable name for the sample files associated 309 * with this image: if `basename(path)`+".gmon" is available, 310 * we use that, otherwise we try iterating through 311 * `basename(path)`+ "~" + NNN + ".gmon" till we get a free 312 * entry. 313 */ 314 if ((sn = basename(pmcstat_string_unintern(pi->pi_execpath))) == NULL) 315 err(EX_OSERR, "ERROR: Cannot process \"%s\"", 316 pmcstat_string_unintern(pi->pi_execpath)); 317 318 nlen = strlen(sn); 319 nlen = min(nlen, (int) (sizeof(name) - sizeof(".gmon"))); 320 321 snprintf(name, sizeof(name), "%.*s.gmon", nlen, sn); 322 323 /* try use the unabridged name first */ 324 if (pmcstat_string_lookup(name) == NULL) 325 pi->pi_samplename = pmcstat_string_intern(name); 326 else { 327 /* 328 * Otherwise use a prefix from the original name and 329 * upto 3 digits. 330 */ 331 nlen = strlen(sn); 332 nlen = min(nlen, (int) (sizeof(name)-sizeof("~NNN.gmon"))); 333 count = 0; 334 do { 335 if (++count > 999) 336 errx(EX_CANTCREAT, "ERROR: cannot create a " 337 "gmon file for \"%s\"", name); 338 snprintf(name, sizeof(name), "%.*s~%3.3d.gmon", 339 nlen, sn, count); 340 if (pmcstat_string_lookup(name) == NULL) { 341 pi->pi_samplename = 342 pmcstat_string_intern(name); 343 count = 0; 344 } 345 } while (count > 0); 346 } 347 348 LIST_INIT(&pi->pi_gmlist); 349 } 350 351 void 352 pmcpl_gmon_shutdownimage(struct pmcstat_image *pi) 353 { 354 struct pmcstat_gmonfile *pgf, *pgftmp; 355 356 LIST_FOREACH_SAFE(pgf, &pi->pi_gmlist, pgf_next, pgftmp) { 357 if (pgf->pgf_file) 358 (void) fclose(pgf->pgf_file); 359 LIST_REMOVE(pgf, pgf_next); 360 free(pgf); 361 } 362 } 363 364 void 365 pmcpl_gmon_newpmc(pmcstat_interned_string ps, struct pmcstat_pmcrecord *pr) 366 { 367 struct stat st; 368 char fullpath[PATH_MAX]; 369 370 (void) pr; 371 372 /* 373 * Create the appropriate directory to hold gmon.out files. 374 */ 375 376 (void) snprintf(fullpath, sizeof(fullpath), "%s/%s", args.pa_samplesdir, 377 pmcstat_string_unintern(ps)); 378 379 /* If the path name exists, it should be a directory */ 380 if (stat(fullpath, &st) == 0 && S_ISDIR(st.st_mode)) 381 return; 382 383 if (mkdir(fullpath, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0) 384 err(EX_OSERR, "ERROR: Cannot create directory \"%s\"", 385 fullpath); 386 } 387 388 /* 389 * Increment the bucket in the gmon.out file corresponding to 'pmcid' 390 * and 'pc'. 391 */ 392 393 void 394 pmcpl_gmon_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr, 395 uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu) 396 { 397 struct pmcstat_pcmap *map; 398 struct pmcstat_image *image; 399 struct pmcstat_gmonfile *pgf; 400 uintfptr_t bucket; 401 HISTCOUNTER *hc; 402 pmc_id_t pmcid; 403 404 (void) nsamples; (void) usermode; (void) cpu; 405 406 map = pmcstat_process_find_map(usermode ? pp : pmcstat_kernproc, cc[0]); 407 if (map == NULL) { 408 /* Unknown offset. */ 409 pmcstat_stats.ps_samples_unknown_offset++; 410 return; 411 } 412 413 assert(cc[0] >= map->ppm_lowpc && cc[0] < map->ppm_highpc); 414 415 image = map->ppm_image; 416 pmcid = pmcr->pr_pmcid; 417 418 /* 419 * If this is the first time we are seeing a sample for 420 * this executable image, try determine its parameters. 421 */ 422 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 423 pmcstat_image_determine_type(image); 424 425 assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN); 426 427 /* Ignore samples in images that we know nothing about. */ 428 if (image->pi_type == PMCSTAT_IMAGE_INDETERMINABLE) { 429 pmcstat_stats.ps_samples_indeterminable++; 430 return; 431 } 432 433 /* 434 * Find the gmon file corresponding to 'pmcid', creating it if 435 * needed. 436 */ 437 pgf = pmcstat_image_find_gmonfile(image, pmcid); 438 if (pgf == NULL) { 439 if ((pgf = calloc(1, sizeof(*pgf))) == NULL) 440 err(EX_OSERR, "ERROR:"); 441 442 pgf->pgf_gmondata = NULL; /* mark as unmapped */ 443 pgf->pgf_name = pmcstat_gmon_create_name(args.pa_samplesdir, 444 image, pmcid); 445 pgf->pgf_pmcid = pmcid; 446 assert(image->pi_end > image->pi_start); 447 pgf->pgf_nbuckets = (image->pi_end - image->pi_start) / 448 FUNCTION_ALIGNMENT; /* see <machine/profile.h> */ 449 pgf->pgf_ndatabytes = sizeof(struct gmonhdr) + 450 pgf->pgf_nbuckets * sizeof(HISTCOUNTER); 451 pgf->pgf_nsamples = 0; 452 pgf->pgf_file = NULL; 453 454 pmcstat_gmon_create_file(pgf, image); 455 456 LIST_INSERT_HEAD(&image->pi_gmlist, pgf, pgf_next); 457 } 458 459 /* 460 * Map the gmon file in if needed. It may have been mapped 461 * out under memory pressure. 462 */ 463 if (pgf->pgf_gmondata == NULL) 464 pmcstat_gmon_map_file(pgf); 465 466 assert(pgf->pgf_gmondata != NULL); 467 468 /* 469 * 470 */ 471 472 bucket = (cc[0] - map->ppm_lowpc) / FUNCTION_ALIGNMENT; 473 474 assert(bucket < pgf->pgf_nbuckets); 475 476 hc = (HISTCOUNTER *) ((uintptr_t) pgf->pgf_gmondata + 477 sizeof(struct gmonhdr)); 478 479 /* saturating add */ 480 if (hc[bucket] < 0xFFFFU) /* XXX tie this to sizeof(HISTCOUNTER) */ 481 hc[bucket]++; 482 else /* mark that an overflow occurred */ 483 pgf->pgf_overflow = 1; 484 485 pgf->pgf_nsamples++; 486 } 487 488 /* 489 * Shutdown module. 490 */ 491 492 void 493 pmcpl_gmon_shutdown(FILE *mf) 494 { 495 int i; 496 struct pmcstat_gmonfile *pgf; 497 struct pmcstat_image *pi; 498 499 /* 500 * Sync back all gprof flat profile data. 501 */ 502 for (i = 0; i < PMCSTAT_NHASH; i++) { 503 LIST_FOREACH(pi, &pmcstat_image_hash[i], pi_next) { 504 if (mf) 505 (void) fprintf(mf, " \"%s\" => \"%s\"", 506 pmcstat_string_unintern(pi->pi_execpath), 507 pmcstat_string_unintern( 508 pi->pi_samplename)); 509 510 /* flush gmon.out data to disk */ 511 LIST_FOREACH(pgf, &pi->pi_gmlist, pgf_next) { 512 pmcstat_gmon_unmap_file(pgf); 513 if (mf) 514 (void) fprintf(mf, " %s/%d", 515 pmcstat_pmcid_to_name( 516 pgf->pgf_pmcid), 517 pgf->pgf_nsamples); 518 if (pgf->pgf_overflow && args.pa_verbosity >= 1) 519 warnx("WARNING: profile \"%s\" " 520 "overflowed.", 521 pmcstat_string_unintern( 522 pgf->pgf_name)); 523 } 524 525 if (mf) 526 (void) fprintf(mf, "\n"); 527 } 528 } 529 530 /* 531 * Compute arcs and add these to the gprof files. 532 */ 533 if (args.pa_flags & FLAG_DO_GPROF && args.pa_graphdepth > 1) 534 pmcstat_callgraph_do_gmon_arcs(); 535 } 536