[de0c3c]: dae / opd_proc.c Maximize Restore History

Download this file

opd_proc.c    1319 lines (1115 with data), 33.2 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
/* $Id: opd_proc.c,v 1.113 2002/05/02 02:19:08 movement Exp $ */
/* COPYRIGHT (C) 2000 THE VICTORIA UNIVERSITY OF MANCHESTER and John Levon
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "opd_proc.h"
/* per-process */
#define OPD_DEFAULT_MAPS 16
#define OPD_MAP_INC 8
/* hash of process lists */
static struct opd_proc *opd_procs[OPD_MAX_PROC_HASH];
/* hash map device mmap */
struct op_hash_index *hashmap;
u32 ctr_count[OP_MAX_COUNTERS];
u8 ctr_event[OP_MAX_COUNTERS];
u8 ctr_um[OP_MAX_COUNTERS];
double cpu_speed;
/* list of images */
static struct list_head opd_images = { &opd_images, &opd_images };
/* Images which belong to the same hash, more than one only if separate_samples
* == 1, are accessed by hash code and linked through the hash_next member
* of opd_image. Hash-less image must be searched through opd_images list */
static struct opd_image * images_with_hash[OP_HASH_MAP_NR];
/* The kernel image is treated separately */
struct opd_image * kernel_image;
/* maintained for statistics purpose only */
static unsigned int nr_images=0;
/**
* opd_print_stats - print out latest statistics
*/
void opd_print_stats(void)
{
struct opd_proc *proc;
int i,j = 0;
for (i=0; i < OPD_MAX_PROC_HASH; i++) {
proc = opd_procs[i];
while (proc) {
++j;
proc = proc->next;
}
}
printf("%s\n", opd_get_time());
printf("Nr. proc struct: %d\n", j);
printf("Nr. image struct: %d\n", nr_images);
printf("Nr. kernel samples: %lu\n", opd_stats[OPD_KERNEL]);
printf("Nr. modules samples: %lu\n", opd_stats[OPD_MODULE]);
printf("Nr. modules samples lost: %lu\n",
opd_stats[OPD_LOST_MODULE]);
printf("Nr. samples lost due to no process information: %lu\n",
opd_stats[OPD_LOST_PROCESS]);
printf("Nr. process samples in user-space: %lu\n", opd_stats[OPD_PROCESS]);
printf("Nr. samples lost due to no map information: %lu\n",
opd_stats[OPD_LOST_MAP_PROCESS]);
if (opd_stats[OPD_PROC_QUEUE_ACCESS]) {
printf("Average depth of search of proc queue: %f\n",
(double)opd_stats[OPD_PROC_QUEUE_DEPTH]
/ (double)opd_stats[OPD_PROC_QUEUE_ACCESS]);
}
if (opd_stats[OPD_MAP_ARRAY_ACCESS]) {
printf("Average depth of iteration through mapping array: %f\n",
(double)opd_stats[OPD_MAP_ARRAY_DEPTH]
/ (double)opd_stats[OPD_MAP_ARRAY_ACCESS]);
}
printf("Nr. sample dumps: %lu\n", opd_stats[OPD_DUMP_COUNT]);
printf("Nr. samples total: %lu\n", opd_stats[OPD_SAMPLES]);
printf("Nr. notifications: %lu\n", opd_stats[OPD_NOTIFICATIONS]);
fflush(stdout);
}
static void opd_delete_proc(struct opd_proc *proc);
/**
* opd_alarm - clean up old procs, msync, and report stats
*/
void opd_alarm(int val __attribute__((unused)))
{
struct opd_proc *proc;
struct opd_proc *next;
uint i;
struct opd_image * image;
struct list_head * pos;
list_for_each(pos, &opd_images) {
image = list_entry(pos, struct opd_image, list_node);
for (i = 0 ; i < op_nr_counters ; ++i) {
struct opd_sample_file * samples =
&image->sample_files[i];
db_sync(&samples->tree);
}
}
for (i=0; i < OPD_MAX_PROC_HASH; i++) {
proc = opd_procs[i];
while (proc) {
next = proc->next;
// delay death whilst its still being accessed
if (proc->dead) {
proc->dead += proc->accessed;
proc->accessed = 0;
if (--proc->dead == 0)
opd_delete_proc(proc);
}
proc=next;
}
}
opd_print_stats();
alarm(60*10);
}
/**
* opd_handle_old_sample_file - deal with old sample file
* @mangled: the sample file name
* @mtime: the new mtime of the binary
*
* If an old sample file exists, verify it is usable.
* If not, move or delete it. Note than at startup the daemon
* check than the last (session) events settings match the
* currents
*/
static void opd_handle_old_sample_file(const char * mangled, time_t mtime)
{
struct opd_header oldheader;
FILE * fp;
fp = fopen(mangled, "r");
if (!fp) {
/* file might not be there, or it just might not be
* openable for some reason, so try to remove anyway
*/
goto del;
}
if (fread(&oldheader, sizeof(struct opd_header), 1, fp) != 1) {
verbprintf("Can't read %s\n", mangled);
goto closedel;
}
if (memcmp(&oldheader.magic, OPD_MAGIC, sizeof(oldheader.magic)) || oldheader.version != OPD_VERSION) {
verbprintf("Magic id check fail for %s\n", mangled);
goto closedel;
}
if (difftime(mtime, oldheader.mtime)) {
verbprintf("mtime differs for %s\n", mangled);
goto closedel;
}
fclose(fp);
verbprintf("Re-using old sample file \"%s\".\n", mangled);
return;
closedel:
fclose(fp);
del:
verbprintf("Deleting old sample file \"%s\".\n", mangled);
remove(mangled);
}
/**
* opd_handle_old_sample_files - deal with old sample files
* @image: the image to check files for
*
* to simplify admin of sample file we rename or remove sample
* files for each counter.
*
* If an old sample file exists, verify it is usable.
* If not, delete it.
*/
static void opd_handle_old_sample_files(const struct opd_image * image)
{
uint i;
char *mangled;
uint len;
const char * app_name = separate_samples ? image->app_name : NULL;
mangled = opd_mangle_filename(image->name, app_name);
len = strlen(mangled);
for (i = 0 ; i < op_nr_counters ; ++i) {
sprintf(mangled + len, "#%d", i);
opd_handle_old_sample_file(mangled, image->mtime);
}
free(mangled);
}
/**
* opd_init_image - init an image sample file
* @image: image to init file for
* @hash: hash of image
* @app_name: the application name where belongs this image
* @name: name of the image to add
* @kernel: is the image a kernel/module image
*
* @image at funtion entry is uninitialised
* @name is copied i.e. should be GC'd separately from the
* image structure if appropriate.
*
* Initialise an opd_image struct for the image @image
* without opening the associated samples files. At return
* the @image is partially initialized.
*/
static void opd_init_image(struct opd_image * image, const char * name,
int hash, const char * app_name, int kernel)
{
list_init(&image->list_node);
image->hash_next = NULL;
image->name = xstrdup(name);
image->kernel = kernel;
image->hash = hash;
/* we do not duplicate this string! */
image->app_name = app_name;
}
/**
* opd_open_image - open an image sample file
* @image: image to open file for
* @hash: hash of image
* @app_name: the application name where belongs this image
* @name: name of the image to add
* @kernel: is the image a kernel/module image
*
* @image at funtion entry is partially initialized by opd_init_image()
*
* Initialise an opd_image struct for the image @image
* without opening the associated samples files. At return
* the @image is fully initialized.
*/
static void opd_open_image(struct opd_image *image)
{
uint i;
verbprintf("Opening image \"%s\" for app \"%s\"\n",
image->name, image->app_name ? image->app_name : "none");
for (i = 0 ; i < op_nr_counters ; ++i) {
memset(&image->sample_files[i], '\0',
sizeof(struct opd_sample_file));
}
image->mtime = opd_get_mtime(image->name);
opd_handle_old_sample_files(image);
/* samples files are lazily openeded */
}
/**
* opd_get_count - retrieve counter value
* @count: raw counter value
*
* Returns the counter value.
*/
inline static u16 opd_get_count(const u16 count)
{
return (count & OP_COUNT_MASK);
}
/**
* opd_get_counter - retrieve counter type
* @count: raw counter value
*
* Returns the counter number (0-N)
*/
inline static u16 opd_get_counter(const u16 count)
{
return OP_COUNTER(count);
}
/*
* opd_open_sample_file - open an image sample file
* @image: image to open file for
* @counter: counter number
*
* Open image sample file for the image @image, counter
* @counter and set up memory mappings for it.
* image->kernel and image->name must have meaningful
* values.
*/
static void opd_open_sample_file(struct opd_image *image, int counter)
{
char * mangled;
struct opd_sample_file *sample_file;
struct opd_header * header;
const char * app_name;
sample_file = &image->sample_files[counter];
app_name = separate_samples ? image->app_name : NULL;
mangled = opd_mangle_filename(image->name, app_name);
sprintf(mangled + strlen(mangled), "#%d", counter);
verbprintf("Opening \"%s\"\n", mangled);
db_open(&sample_file->tree, mangled, DB_RDWR, sizeof(struct opd_header));
if (!sample_file->tree.base_memory) {
fprintf(stderr,
"oprofiled: db_open() of image sample file \"%s\" failed: %s\n",
mangled, strerror(errno));
goto err;
}
header = sample_file->tree.base_memory;
memset(header, '\0', sizeof(struct opd_header));
header->version = OPD_VERSION;
memcpy(header->magic, OPD_MAGIC, sizeof(header->magic));
header->is_kernel = image->kernel;
header->ctr_event = ctr_event[counter];
header->ctr_um = ctr_um[counter];
header->ctr = counter;
header->cpu_type = cpu_type;
header->ctr_count = ctr_count[counter];
header->cpu_speed = cpu_speed;
header->mtime = image->mtime;
header->separate_samples = separate_samples;
err:
free(mangled);
}
/**
* opd_reopen_sample_files - re-open all sample files
*
* In fact we just close them, and re-open them lazily
* as usual.
*/
void opd_reopen_sample_files(void)
{
struct list_head * pos;
list_for_each(pos, &opd_images) {
struct opd_image * image =
list_entry(pos, struct opd_image, list_node);
unsigned int i;
for (i = 0 ; i < op_nr_counters ; ++i) {
struct opd_sample_file * samples =
&image->sample_files[i];
db_close(&samples->tree);
}
}
}
/**
* opd_check_image_mtime - ensure samples file is up to date
* @image: image to check
*/
static void opd_check_image_mtime(struct opd_image * image)
{
uint i;
char *mangled;
uint len;
time_t newmtime = opd_get_mtime(image->name);
const char * app_name;
if (image->mtime == newmtime)
return;
verbprintf("Current mtime %lu differs from stored "
"mtime %lu for %s\n", newmtime, image->mtime, image->name);
app_name = separate_samples ? image->app_name : NULL;
mangled = opd_mangle_filename(image->name, app_name);
len = strlen(mangled);
for (i=0; i < op_nr_counters; i++) {
struct opd_sample_file * file = &image->sample_files[i];
if (file->tree.base_memory) {
db_close(&file->tree);
}
sprintf(mangled + len, "#%d", i);
verbprintf("Deleting out of date \"%s\"\n", mangled);
remove(mangled);
}
free(mangled);
opd_open_image(image);
}
/**
* opd_put_image_sample - write sample to file
* @image: image for sample
* @offset: (file) offset to write to
* @count: raw counter value
*
* Add to the count stored at position @offset in the
* image file. Overflow pins the count at the maximum
* value.
*
* @count is the raw value passed from the kernel.
*/
void opd_put_image_sample(struct opd_image *image, u32 offset, u16 count)
{
struct opd_sample_file* sample_file;
int counter;
counter = opd_get_counter(count);
sample_file = &image->sample_files[counter];
if (!sample_file->tree.base_memory) {
opd_open_sample_file(image, counter);
if (sample_file->tree.base_memory) {
/* opd_open_sample_file output an error message */
return;
}
}
db_insert(&sample_file->tree, offset, opd_get_count(count));
}
/**
* opd_init_images - initialise image structure
*
* Initialise the global image structure, reserving the
* first entry for the kernel.
*/
void opd_init_images(void)
{
/* the kernel image is treated a part the list of image */
kernel_image = xmalloc(sizeof(struct opd_image));
opd_init_image(kernel_image, vmlinux, -1, NULL, 1);
opd_open_image(kernel_image);
}
/**
* opd_add_image - add an image to the image structure
* @name: name of the image to add
* @hash: hash of image
* @app_name: the application name where belongs this image
* @kernel: is the image a kernel/module image
*
* Add an image to the image structure with name @name.
*
* @name is copied i.e. should be GC'd separately from the
* image structure if appropriate.
*
* The new image pointer is returned.
*/
static struct opd_image * opd_add_image(const char * name, int hash, const char * app_name, int kernel)
{
struct opd_image * image = xmalloc(sizeof(struct opd_image));
opd_init_image(image, name, hash, app_name, kernel);
list_add(&image->list_node, &opd_images);
/* image with hash -1 are lazilly put in the images_with_hash array */
if (hash != -1) {
image->hash_next = images_with_hash[hash];
images_with_hash[hash] = image;
}
nr_images++;
opd_open_image(image);
return image;
}
/**
* bstreq - reverse string comparison
* @str1: first string
* @str2: second string
*
* Compares two strings, starting at the end.
* Returns %1 if they match, %0 otherwise.
*/
int bstreq(const char *str1, const char *str2)
{
char *a = (char *)str1;
char *b = (char *)str2;
while (*a && *b)
a++,b++;
if (*a || *b)
return 0;
while (a != str1) {
if (*b-- != *a--)
return 0;
}
return 1;
}
/**
* opd_app_name - get the application name or %NULL if irrelevant
* @proc: the process to examine
*
* Returns the app_name for the given @proc or %NULL if
* it does not exist any mapping for this proc (which is
* true for the first mapping at exec time)
*/
static inline const char * opd_app_name(const struct opd_proc * proc)
{
const char * app_name = NULL;
if (proc->nr_maps)
app_name = proc->maps[0].image->name;
return app_name;
}
/**
* opd_find_image - find an image
* @name: name of image to find
* @hash: hash of image to find
* @app_name: the application name where belongs this image
*
* Returns the image pointer for the file specified by @name, or %NULL.
*/
/*
* We make here a linear search through the whole image list. There is no need
* to improve performance, only /proc parsed app are hashless and when they
* are found one time by this function they receive a valid hash code. */
static struct opd_image * opd_find_image(const char * name, int hash, const char * app_name)
{
struct opd_image * image = 0; /* supress warn non initialized use */
struct list_head * pos;
list_for_each(pos, &opd_images) {
image = list_entry(pos, struct opd_image, list_node);
if (bstreq(image->name, name)) {
if (!separate_samples)
break;
if (image->app_name == NULL && app_name == NULL)
break;
if (image->app_name != NULL && app_name != NULL &&
bstreq(image->app_name, app_name))
break;
}
}
if (pos != &opd_images) {
/* we can have hashless images from /proc/pid parsing, modules
* are handled in a separate list */
if (hash != -1) {
image->hash = hash;
if (image->hash_next) { /* parano check */
printf("error: image->hash_next != NULL and image->hash == -1\n");
exit(EXIT_FAILURE);
}
image->hash_next = images_with_hash[hash];
images_with_hash[hash] = image;
}
/* The app_name field is always valid */
return image;
}
return NULL;
}
/**
* opd_get_image_by_hash - get an image from the image
* structure by hash value
* @hash: hash of the image to get
* @app_name: the application name where belongs this image
*
* Get the image specified by @hash and @app_name
* if present, else return %NULL
*/
static struct opd_image * opd_get_image_by_hash(int hash, const char * app_name)
{
struct opd_image * image;
for (image = images_with_hash[hash]; image != NULL; image = image->hash_next) {
if (!separate_samples)
break;
if (image->app_name == NULL && app_name == NULL)
break;
if (image->app_name != NULL && app_name != NULL &&
bstreq(image->app_name, app_name))
break;
}
return image;
}
/**
* opd_get_image - get an image from the image structure
* @name: name of image
* @hash: hash of the image to get
* @app_name: the application name where belongs this image
* @kernel: is the image a kernel/module image
*
* Get the image specified by the file name @name from the
* image structure. If it is not present, the image is
* added to the structure. In either case, the image number
* is returned.
*/
struct opd_image * opd_get_image(const char * name, int hash, const char * app_name, int kernel)
{
struct opd_image * image;
if ((image = opd_find_image(name, hash, app_name)) == NULL)
image = opd_add_image(name, hash, app_name, kernel);
return image;
}
/**
* opd_new_proc - create a new process structure
* @prev: previous list entry
* @next: next list entry
*
* Allocate and initialise a process structure and insert
* it into the the list point specified by @prev and @next.
*/
static struct opd_proc * opd_new_proc(struct opd_proc *prev, struct opd_proc *next)
{
struct opd_proc *proc;
proc = xmalloc(sizeof(struct opd_proc));
proc->maps = NULL;
proc->pid = 0;
proc->nr_maps = 0;
proc->max_nr_maps = 0;
proc->last_map = 0;
proc->dead = 0;
proc->accessed = 0;
proc->prev = prev;
proc->next = next;
return proc;
}
/**
* proc_hash - hash pid value
* @pid: pid value to hash
*
*/
inline static uint proc_hash(u16 pid)
{
return ((pid>>4) ^ (pid)) % OPD_MAX_PROC_HASH;
}
/**
* opd_delete_proc - delete a process
* @proc: process to delete
*
* Remove the process @proc from the process list and free
* the associated structures.
*/
static void opd_delete_proc(struct opd_proc *proc)
{
if (!proc->prev)
opd_procs[proc_hash(proc->pid)] = proc->next;
else
proc->prev->next = proc->next;
if (proc->next)
proc->next->prev = proc->prev;
if (proc->maps) free(proc->maps);
free(proc);
}
/**
* opd_init_maps - initialise map structure for a process
* @proc: process to work on
*
* Initialise the mapping info structure for process @proc.
*/
static void opd_init_maps(struct opd_proc *proc)
{
proc->maps = xcalloc(sizeof(struct opd_map), OPD_DEFAULT_MAPS);
proc->max_nr_maps = OPD_DEFAULT_MAPS;
proc->nr_maps = 0;
proc->last_map = 0;
}
/**
* opd_add_proc - add a process
* @pid: process id
*
* Create a new process structure and add it
* to the head of the process list. The process structure
* is filled in as appropriate.
*
*/
static struct opd_proc *opd_add_proc(u16 pid)
{
struct opd_proc *proc;
uint hash = proc_hash(pid);
proc=opd_new_proc(NULL, opd_procs[hash]);
if (opd_procs[hash])
opd_procs[hash]->prev = proc;
opd_procs[hash] = proc;
opd_init_maps(proc);
proc->pid = pid;
return proc;
}
/**
* opd_grow_maps - grow map structure for a process
* @proc: process to work on
*
* Grow the map structure for a process by %OPD_MAP_INC
* entries.
*/
static void opd_grow_maps(struct opd_proc *proc)
{
proc->maps = xrealloc(proc->maps, sizeof(struct opd_map)*(proc->max_nr_maps+OPD_MAP_INC));
proc->max_nr_maps += OPD_MAP_INC;
}
/**
* opd_kill_maps - delete mapping information for a process
* @proc: process to work on
*
* Frees structures holding mapping information and resets
* the values, allocating a new map structure.
*/
static void opd_kill_maps(struct opd_proc *proc)
{
if (proc->maps)
free(proc->maps);
opd_init_maps(proc);
}
/**
* opd_do_proc_lru - rework process list
* @head: head of process list
* @proc: process to move
*
* Perform LRU on the process list by moving it to
* the head of the process list.
*/
inline static void opd_do_proc_lru(struct opd_proc **head, struct opd_proc *proc)
{
if (proc->prev) {
proc->prev->next = proc->next;
if (proc->next)
proc->next->prev = proc->prev;
(*head)->prev = proc;
proc->prev = NULL;
proc->next = *head;
(*head) = proc;
}
}
/**
* opd_get_proc - get process from process list
* @pid: pid to search for
*
* A process with pid @pid is searched on the process list,
* maintaining LRU order. If it is not found, %NULL is returned,
* otherwise the process structure is returned.
*/
static struct opd_proc *opd_get_proc(u16 pid)
{
struct opd_proc *proc;
proc = opd_procs[proc_hash(pid)];
opd_stats[OPD_PROC_QUEUE_ACCESS]++;
while (proc) {
if (pid == proc->pid) {
opd_do_proc_lru(&opd_procs[proc_hash(pid)],proc);
return proc;
}
opd_stats[OPD_PROC_QUEUE_DEPTH]++;
proc = proc->next;
}
return NULL;
}
/**
* opd_is_in_map - check whether an EIP is within a mapping
* @map: map to check
* @eip: EIP value
*
* Return %TRUE if the EIP value @eip is within the boundaries
* of the map @map, %FALSE otherwise.
*/
inline static int opd_is_in_map(struct opd_map *map, u32 eip)
{
return (eip >= map->start && eip < map->end);
}
/**
* verb_show_sample - print the sample out to the log
* @offset: the offset value
* @map: map to print
* @last_map: previous map used
*/
inline static void verb_show_sample(u32 offset, struct opd_map *map, const char * last_map)
{
verbprintf("DO_PUT_SAMPLE %s: calc offset 0x%.8x, map start 0x%.8x,"
" end 0x%.8x, offset 0x%.8x, name \"%s\"\n",
last_map, offset, map->start, map->end, map->offset, map->image->name);
}
/**
* opd_map_offset - return offset of sample against map
* @map: map to use
* @eip: EIP value to use
*
* Returns the offset of the EIP value @eip into
* the map @map, which is the same as the file offset
* for the relevant binary image.
*/
inline static u32 opd_map_offset(struct opd_map *map, u32 eip)
{
return (eip - map->start) + map->offset;
}
/**
* opd_eip_is_kernel - is the sample from kernel/module space
* @eip: EIP value
*
* Returns %TRUE if @eip is in the address space starting at
* kernel_start, %FALSE otherwise.
*/
inline static int opd_eip_is_kernel(u32 eip)
{
extern u32 kernel_start;
return (eip >= kernel_start);
}
/**
* opd_put_sample - process a sample
* @sample: sample to process
*
* Write out the sample to the appropriate sample file. This
* routine handles kernel and module samples as well as ordinary ones.
*/
void opd_put_sample(const struct op_sample *sample)
{
unsigned int i;
struct opd_proc *proc;
opd_stats[OPD_SAMPLES]++;
verbprintf("DO_PUT_SAMPLE: c%d, EIP 0x%.8x, pid %.6d, count %.6d\n",
opd_get_counter(sample->count), sample->eip, sample->pid, sample->count);
if (opd_eip_is_kernel(sample->eip)) {
opd_handle_kernel_sample(sample->eip, sample->count);
return;
}
if (kernel_only)
return;
if (!(proc = opd_get_proc(sample->pid))) {
verbprintf("No proc info for pid %.6d.\n", sample->pid);
opd_stats[OPD_LOST_PROCESS]++;
return;
}
proc->accessed = 1;
if (!proc->nr_maps)
goto out;
/* proc->last_map is always safe as mappings are never deleted except by
* things which reset last_map. If last map is the primary image, we use it
* anyway (last_map == 0).
*/
opd_stats[OPD_MAP_ARRAY_ACCESS]++;
if (opd_is_in_map(&proc->maps[proc->last_map], sample->eip)) {
i = proc->last_map;
if (proc->maps[i].image != NULL) {
verb_show_sample(opd_map_offset(&proc->maps[i], sample->eip),
&proc->maps[i], "(LAST MAP)");
opd_put_image_sample(proc->maps[i].image,
opd_map_offset(&proc->maps[i], sample->eip), sample->count);
}
opd_stats[OPD_PROCESS]++;
return;
}
/* look for which map and find offset. We search backwards in order to prefer
* more recent mappings (which means we don't need to intercept munmap)
*/
for (i=proc->nr_maps; i > 0; i--) {
int const map = i - 1;
if (opd_is_in_map(&proc->maps[map], sample->eip)) {
u32 offset = opd_map_offset(&proc->maps[map], sample->eip);
if (proc->maps[map].image != NULL) {
verb_show_sample(offset, &proc->maps[map], "");
opd_put_image_sample(proc->maps[map].image, offset, sample->count);
}
proc->last_map = map;
opd_stats[OPD_PROCESS]++;
return;
}
opd_stats[OPD_MAP_ARRAY_DEPTH]++;
}
out:
/* couldn't locate it */
verbprintf("Couldn't find map for pid %.6d, EIP 0x%.8x.\n", sample->pid, sample->eip);
opd_stats[OPD_LOST_MAP_PROCESS]++;
return;
}
/**
* opd_put_mapping - add a mapping to a process
* @proc: process to add map to
* @image: mapped image pointer
* @start: start of mapping
* @offset: file offset of mapping
* @end: end of mapping
*
* Add the mapping specified to the process @proc.
*/
static void opd_put_mapping(struct opd_proc *proc, struct opd_image * image, u32 start, u32 offset, u32 end)
{
verbprintf("Placing mapping for process %d: 0x%.8x-0x%.8x, off 0x%.8x, \"%s\" at maps pos %d\n",
proc->pid, start, end, offset, image->name, proc->nr_maps);
opd_check_image_mtime(image);
proc->maps[proc->nr_maps].image = image;
proc->maps[proc->nr_maps].start = start;
proc->maps[proc->nr_maps].offset = offset;
proc->maps[proc->nr_maps].end = end;
if (++proc->nr_maps == proc->max_nr_maps)
opd_grow_maps(proc);
/* we reset last map here to force searching backwards */
proc->last_map = 0;
}
/**
* opd_handle_fork - deal with fork notification
* @note: note to handle
*
* Deal with a fork() notification by creating a new process
* structure, and copying mapping information from the old process.
*
* sample->pid contains the process id of the old process.
* sample->eip contains the process id of the new process.
*/
void opd_handle_fork(const struct op_note *note)
{
struct opd_proc *old;
struct opd_proc *proc;
verbprintf("DO_FORK: from %d to %d\n", note->pid, note->addr);
old = opd_get_proc(note->pid);
/* we can quite easily get a fork() after the execve() because the notifications
* are racy. In particular, the fork notification is done on parent return (so we
* know the pid), but this will often be after the execve is done by the child.
*
* So we only create a new setup if it doesn't exist already, allowing
* both the clone() and the execve() cases to work.
*/
if (opd_get_proc((u16)note->addr))
return;
/* eip is actually pid of new process */
proc = opd_add_proc((u16)note->addr);
if (!old)
return;
/* remove the kernel map and copy over */
if (proc->maps) free(proc->maps);
proc->maps = xmalloc(sizeof(struct opd_map) * old->max_nr_maps);
memcpy(proc->maps,old->maps,sizeof(struct opd_map) * old->nr_maps);
proc->nr_maps = old->nr_maps;
proc->max_nr_maps = old->max_nr_maps;
}
/**
* opd_handle_exit - deal with exit notification
* @note: note to handle
*
* Deal with an exit() notification by setting the flag "dead"
* on a process. These will be later cleaned up by the %SIGALRM
* handler.
*
* sample->pid contains the process id of the exited process.
*/
void opd_handle_exit(const struct op_note *note)
{
struct opd_proc *proc;
verbprintf("DO_EXIT: process %d\n", note->pid);
proc = opd_get_proc(note->pid);
if (proc) {
proc->dead = 1;
proc->accessed = 1;
} else {
verbprintf("unknown proc %u just exited.\n", note->pid);
}
}
/**
* get_from_pool - retrieve string from hash map pool
* @ind: index into pool
*/
inline static char * get_from_pool(uint ind)
{
return ((char *)(hashmap + OP_HASH_MAP_NR) + ind);
}
/**
* opd_handle_hashmap - parse image from kernel hash map
* @hash: hash value
* @app_name: the application name which belongs this image
*
* Finds an image from its name.
*/
static struct opd_image * opd_handle_hashmap(int hash, const char * app_name)
{
char file[PATH_MAX];
char *c = &file[PATH_MAX-1];
int orighash = hash;
*c = '\0';
while (hash) {
char * name = get_from_pool(hashmap[hash].name);
if (strlen(name) + 1 + strlen(c) >= PATH_MAX) {
fprintf(stderr,"String \"%s\" too large.\n", c);
exit(EXIT_FAILURE);
}
c -= strlen(name) + 1;
*c = '/';
strncpy(c + 1, name, strlen(name));
/* move onto parent */
hash = hashmap[hash].parent;
}
return opd_get_image(c, orighash, app_name, 0);
}
/**
* opd_handle_mapping - deal with mapping notification
* @note: mapping notification
*
* Deal with one notification that a process has mapped
* in a new executable file. The mapping information is
* added to the process structure.
*/
void opd_handle_mapping(const struct op_note *note)
{
struct opd_proc *proc;
struct opd_image * image;
int hash;
const char * app_name;
proc = opd_get_proc(note->pid);
if (!proc) {
verbprintf("Told about mapping for non-existent process %u.\n", note->pid);
proc = opd_add_proc(note->pid);
}
hash = note->hash;
if (hash == -1) {
/* possibly deleted file */
return;
}
if (hash < 0 || hash >= OP_HASH_MAP_NR) {
fprintf(stderr,"hash value %u out of range.\n",hash);
return;
}
app_name = opd_app_name(proc);
image = opd_get_image_by_hash(hash, app_name);
if (image == NULL)
image = opd_handle_hashmap(hash, app_name);
opd_put_mapping(proc, image, note->addr, note->offset, note->addr + note->len);
}
/**
* opd_handle_exec - deal with notification of execve()
* @pid: pid of execve()d process
*
* Drop all mapping information for the process.
*/
void opd_handle_exec(u16 pid)
{
struct opd_proc *proc;
verbprintf("DO_EXEC: pid %u\n", pid);
/* There is a race for samples received between fork/exec sequence.
* These samples belong to the old mapping but we can not say if
* samples has been received before the exec or after. This explain
* the message "Couldn't find map for ..." in verbose mode.
*
* Unhopefully it is difficult to get an estimation of these misplaced
* samples, the error message can count only out of mapping samples but
* not samples between the race and inside the mapping of the exec'ed
* process :/.
*
* Trying to save old mapping is not correct due the above reason. The
* only manner to handle this is to flush the module samples hash table
* after each fork which is unacceptable for performance reasons */
proc = opd_get_proc(pid);
if (proc)
opd_kill_maps(proc);
else
opd_add_proc(pid);
}
/**
* opd_add_ascii_map - parse an ASCII map string for a process
* @proc: process to add map to
* @line: 0-terminated ASCII string
*
* Attempt to parse the string @line for map information
* and add the info to the process @proc. Returns %TRUE
* on success, %FALSE otherwise.
*
* The parsing is based on Linux 2.4 format, which looks like this :
*
* 4001e000-400fc000 r-xp 00000000 03:04 31011 /lib/libc-2.1.2.so
*/
/* FIXME: handle (deleted) */
static int opd_add_ascii_map(struct opd_proc *proc, const char *line)
{
struct opd_map *map = &proc->maps[proc->nr_maps];
const char *cp = line;
/* skip to protection field */
while (*cp && *cp != ' ')
cp++;
/* handle rwx */
if (!*cp || (!*(++cp)) || (!*(++cp)) || (*(++cp) != 'x'))
return FALSE;
/* get start and end from "40000000-4001f000" */
if (sscanf(line,"%x-%x", &map->start, &map->end) != 2)
return FALSE;
/* "p " */
cp += 2;
/* read offset */
if (sscanf(cp,"%x", &map->offset) != 1)
return FALSE;
while (*cp && *cp != '/')
cp++;
if (!*cp)
return FALSE;
/* FIXME: we should verify this is indeed the primary
* app image by readlinking /proc/pid/exe */
map->image = opd_get_image(cp, -1, opd_app_name(proc), 0);
if (!map->image)
return FALSE;
if (++proc->nr_maps == proc->max_nr_maps)
opd_grow_maps(proc);
return TRUE;
}
/**
* opd_get_ascii_maps - read all maps for a process
* @proc: process to work on
*
* Read the /proc/<pid>/maps file and add all
* mapping information found to the process @proc.
*/
static void opd_get_ascii_maps(struct opd_proc *proc)
{
FILE *fp;
char mapsfile[20] = "/proc/";
char *line;
snprintf(mapsfile + 6, 6, "%hu", proc->pid);
strcat(mapsfile,"/maps");
fp = opd_try_open_file(mapsfile, "r");
if (!fp)
return;
while (1) {
line = opd_get_line(fp);
if (streq(line, "") && feof(fp)) {
free(line);
break;
} else {
opd_add_ascii_map(proc, line);
free(line);
}
}
opd_close_file(fp);
}
/**
* opd_get_ascii_procs - read process and mapping information from /proc
*
* Read information on each process and its mappings from the /proc
* filesystem.
*/
void opd_get_ascii_procs(void)
{
DIR *dir;
struct dirent *dirent;
struct opd_proc *proc;
u16 pid;
if (!(dir = opendir("/proc"))) {
perror("oprofiled: /proc directory could not be opened. ");
exit(EXIT_FAILURE);
}
while ((dirent = readdir(dir))) {
if (sscanf(dirent->d_name, "%hu", &pid) == 1) {
verbprintf("ASCII added %u\n", pid);
proc = opd_add_proc(pid);
opd_get_ascii_maps(proc);
}
}
closedir(dir);
}
/**
* opd_proc_cleanup - clean up on exit
*/
void opd_proc_cleanup(void)
{
struct opd_image * image = 0;
struct list_head * pos, * pos2;
uint i;
list_for_each_safe(pos, pos2, &opd_images) {
image = list_entry(pos, struct opd_image, list_node);
if (image->name)
free(image->name);
free(image);
}
free(kernel_image->name);
free(kernel_image);
for (i=0; i < OPD_MAX_PROC_HASH; i++) {
struct opd_proc * proc = opd_procs[i];
struct opd_proc * next;
while (proc) {
next = proc->next;
opd_delete_proc(proc);
proc=next;
}
}
opd_clear_module_info();
}