00001
00012 #include "op_hw_config.h"
00013 #include "opd_proc.h"
00014 #include "opd_image.h"
00015 #include "opd_mapping.h"
00016 #include "opd_sample_files.h"
00017 #include "opd_kernel.h"
00018 #include "opd_24_stats.h"
00019 #include "opd_printf.h"
00020 #include "oprofiled.h"
00021
00022 #include "op_interface.h"
00023 #include "op_libiberty.h"
00024
00025 #include <sys/types.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029
00030
00031 #define OPD_MAX_PROC_HASH 1024
00032
00033 extern int cpu_number;
00034
00035
00036 static struct list_head opd_procs[OPD_MAX_PROC_HASH];
00037
00038
00039 static int nr_procs;
00040
00041
00042 void opd_init_procs(void)
00043 {
00044 int i;
00045 for (i = 0; i < OPD_MAX_PROC_HASH; i++)
00046 list_init(&opd_procs[i]);
00047 }
00048
00049
00050 int opd_get_nr_procs(void)
00051 {
00052 return nr_procs;
00053 }
00054
00055
00061 inline static uint proc_hash(pid_t tid)
00062 {
00063
00064 return ((tid >> 4) ^ (tid)) % OPD_MAX_PROC_HASH;
00065 }
00066
00067
00068 struct opd_proc * opd_new_proc(pid_t tid, pid_t tgid)
00069 {
00070 struct opd_proc * proc;
00071
00072 nr_procs++;
00073 proc = xmalloc(sizeof(struct opd_proc));
00074 list_init(&proc->maps);
00075 proc->name = NULL;
00076 proc->tid = tid;
00077 proc->tgid = tgid;
00078 proc->dead = 0;
00079 proc->accessed = 0;
00080 list_add(&proc->next, &opd_procs[proc_hash(tid)]);
00081 return proc;
00082 }
00083
00084
00085 struct opd_proc * opd_get_proc(pid_t tid, pid_t tgid)
00086 {
00087 struct opd_proc * proc;
00088 uint hash = proc_hash(tid);
00089 struct list_head * pos, *pos2;
00090
00091 opd_24_stats[OPD_PROC_QUEUE_ACCESS]++;
00092 list_for_each_safe(pos, pos2, &opd_procs[hash]) {
00093 opd_24_stats[OPD_PROC_QUEUE_DEPTH]++;
00094 proc = list_entry(pos, struct opd_proc, next);
00095 if (tid == proc->tid && tgid == proc->tgid) {
00096
00097 list_del(&proc->next);
00098 list_add(&proc->next, &opd_procs[hash]);
00099 return proc;
00100 }
00101 }
00102
00103 return NULL;
00104 }
00105
00106
00112 inline static void
00113 verb_show_sample(unsigned long offset, struct opd_map * map)
00114 {
00115 verbprintf(vsamples, "DO_PUT_SAMPLE : calc offset 0x%.8lx, "
00116 "map start 0x%.8lx, end 0x%.8lx, offset 0x%.8lx, name \"%s\"\n",
00117 offset, map->start, map->end, map->offset,
00118 map->image->name);
00119 }
00120
00121
00122 void opd_put_image_sample(struct opd_image * image, unsigned long offset,
00123 u32 counter)
00124 {
00125 struct opd_24_sfile * sfile;
00126 int err;
00127
00128 if (image->ignored)
00129 return;
00130
00131 if (!image->sfiles[cpu_number]) {
00132 image->sfiles[cpu_number] =
00133 xcalloc(OP_MAX_COUNTERS, sizeof(struct op_24_sfile *));
00134 }
00135 sfile = image->sfiles[cpu_number][counter];
00136
00137 if (!sfile || !odb_open_count(&sfile->sample_file)) {
00138 if (opd_open_24_sample_file(image, counter, cpu_number)) {
00139
00140 opd_24_stats[OPD_LOST_SAMPLEFILE]++;
00141 return;
00142 }
00143 sfile = image->sfiles[cpu_number][counter];
00144 }
00145
00146 err = odb_update_node(&sfile->sample_file, offset);
00147 if (err) {
00148 fprintf(stderr, "%s\n", strerror(err));
00149 abort();
00150 }
00151
00152 opd_24_sfile_lru(sfile);
00153 }
00154
00155
00164 static int opd_lookup_maps(struct opd_proc * proc,
00165 struct op_sample const * sample)
00166 {
00167 struct list_head * pos;
00168
00169 proc->accessed = 1;
00170
00171 opd_24_stats[OPD_MAP_ARRAY_ACCESS]++;
00172 list_for_each(pos, &proc->maps) {
00173 struct opd_map * map = list_entry(pos, struct opd_map, next);
00174 if (opd_is_in_map(map, sample->eip)) {
00175 unsigned long offset = opd_map_offset(map, sample->eip);
00176 if (map->image != NULL) {
00177 verb_show_sample(offset, map);
00178 opd_put_image_sample(map->image, offset, sample->counter);
00179 }
00180 opd_24_stats[OPD_PROCESS]++;
00181 return 1;
00182 }
00183 opd_24_stats[OPD_MAP_ARRAY_DEPTH]++;
00184 }
00185
00186 return 0;
00187 }
00188
00189
00190 void opd_put_sample(struct op_sample const * sample)
00191 {
00192 struct opd_proc * proc;
00193 int in_kernel_eip = opd_eip_is_kernel(sample->eip);
00194
00195 opd_24_stats[OPD_SAMPLES]++;
00196
00197 verbprintf(vsamples, "DO_PUT_SAMPLE: c%d, EIP 0x%.8lx, tgid %.6d pid %.6d\n",
00198 sample->counter, sample->eip, sample->tgid, sample->pid);
00199
00200 if (!separate_kernel && in_kernel_eip) {
00201 opd_handle_kernel_sample(sample->eip, sample->counter);
00202 return;
00203 }
00204
00205 if (!(proc = opd_get_proc(sample->pid, sample->tgid))) {
00206 if (in_kernel_eip || no_vmlinux) {
00207
00208
00209
00210
00211
00212
00213 opd_handle_kernel_sample(sample->eip, sample->counter);
00214 } else {
00215 verbprintf(vmisc, "No proc info for tgid %.6d pid %.6d.\n",
00216 sample->tgid, sample->pid);
00217 opd_24_stats[OPD_LOST_PROCESS]++;
00218 }
00219 return;
00220 }
00221
00222 if (opd_lookup_maps(proc, sample))
00223 return;
00224
00225 if (in_kernel_eip) {
00226 opd_add_kernel_map(proc, sample->eip);
00227 if (opd_lookup_maps(proc, sample))
00228 return;
00229 }
00230
00231
00232 verbprintf(vsamples, "Couldn't find map for pid %.6d, EIP 0x%.8lx.\n",
00233 sample->pid, sample->eip);
00234 opd_24_stats[OPD_LOST_MAP_PROCESS]++;
00235 }
00236
00237
00238 void opd_handle_fork(struct op_note const * note)
00239 {
00240 struct opd_proc * old;
00241 struct opd_proc * proc;
00242 struct list_head * pos;
00243
00244 verbprintf(vmisc, "DO_FORK: from %d, %d to %ld, %ld\n", note->pid, note->tgid,
00245 note->addr, note->len);
00246
00247 old = opd_get_proc(note->pid, note->tgid);
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 if (opd_get_proc(note->addr, note->len))
00258 return;
00259
00260
00261 proc = opd_new_proc(note->addr, note->len);
00262
00263 if (!old)
00264 return;
00265
00266
00267 list_for_each(pos, &old->maps) {
00268 struct opd_map * map = list_entry(pos, struct opd_map, next);
00269 if (!separate_thread) {
00270 opd_add_mapping(proc, map->image, map->start,
00271 map->offset, map->end);
00272 } else {
00273
00274
00275
00276 struct opd_image * image =
00277 opd_get_image(map->image->name, old->name,
00278 map->image->kernel, note->addr, note->len);
00279 opd_add_mapping(proc, image, map->start, map->offset,
00280 map->end);
00281 }
00282 }
00283 }
00284
00285
00286 void opd_handle_exec(pid_t tid, pid_t tgid)
00287 {
00288 struct opd_proc * proc;
00289
00290 verbprintf(vmisc, "DO_EXEC: pid %u %u\n", tid, tgid);
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305 proc = opd_get_proc(tid, tgid);
00306 if (proc) {
00307 opd_kill_maps(proc);
00308
00309 free((char *)proc->name);
00310 proc->name = NULL;
00311 } else {
00312 opd_new_proc(tid, tgid);
00313 }
00314 }
00315
00316
00317 void opd_handle_exit(struct op_note const * note)
00318 {
00319 struct opd_proc * proc;
00320
00321 verbprintf(vmisc, "DO_EXIT: process %d\n", note->pid);
00322
00323 proc = opd_get_proc(note->pid, note->tgid);
00324 if (proc) {
00325 proc->dead = 1;
00326 proc->accessed = 1;
00327 } else {
00328 verbprintf(vmisc, "unknown proc %u just exited.\n", note->pid);
00329 }
00330 }
00331
00332
00333 typedef void (*opd_proc_cb)(struct opd_proc *);
00334
00341 static void opd_for_each_proc(opd_proc_cb proc_cb)
00342 {
00343 struct list_head * pos;
00344 struct list_head * pos2;
00345 int i;
00346
00347 for (i = 0; i < OPD_MAX_PROC_HASH; ++i) {
00348 list_for_each_safe(pos, pos2, &opd_procs[i]) {
00349 struct opd_proc * proc =
00350 list_entry(pos, struct opd_proc, next);
00351 proc_cb(proc);
00352 }
00353 }
00354 }
00355
00356
00364 static void opd_delete_proc(struct opd_proc * proc)
00365 {
00366 --nr_procs;
00367 list_del(&proc->next);
00368 opd_kill_maps(proc);
00369 if (proc->name)
00370 free((char *)proc->name);
00371 free(proc);
00372 }
00373
00374
00375 void opd_proc_cleanup(void)
00376 {
00377 opd_for_each_proc(opd_delete_proc);
00378 }
00379
00380
00388 static void opd_age_proc(struct opd_proc * proc)
00389 {
00390
00391 if (proc->dead) {
00392 proc->dead += proc->accessed;
00393 proc->accessed = 0;
00394 if (--proc->dead == 0)
00395 opd_delete_proc(proc);
00396 }
00397 }
00398
00399
00400 void opd_age_procs(void)
00401 {
00402 opd_for_each_proc(opd_age_proc);
00403 }
00404
00405
00414 static void opd_remove_kernel_mapping(struct opd_proc * proc)
00415 {
00416 struct list_head * pos, * pos2;
00417
00418 list_for_each_safe(pos, pos2, &proc->maps) {
00419 struct opd_map * map = list_entry(pos, struct opd_map, next);
00420 if (opd_eip_is_kernel(map->start + map->offset)) {
00421 list_del(pos);
00422 opd_delete_image(map->image);
00423 free(map);
00424 }
00425 }
00426 }
00427
00428
00429 void opd_clear_kernel_mapping(void)
00430 {
00431 opd_for_each_proc(opd_remove_kernel_mapping);
00432 }