szaydel
7/5/2017 - 12:46 PM

[Dtrace snippets for looking at processes] Scripts and one-liners for getting insights from processes in userspace #tags: dtrace, process, e

[Dtrace snippets for looking at processes] Scripts and one-liners for getting insights from processes in userspace #tags: dtrace, process, execname, exec, syscall

dtrace -qn '
  long r, w; 
  BEGIN { printf("Avg. KB/s\n"); }
  fbt::read:entry { r += args[2]; }
  fbt::write:entry { w += args[2]; }
  tick-1sec {
    @r = avg(r); @w = avg(w); r = 0; w = 0;
  } tick-10sec {
    normalize(@r, 1024); normalize(@w, 1024);
    printa("read=%@d write=%@d\n", @r, @w); 
    trunc(@r); trunc(@w);
  }' -p <process_id>
dtrace -qn '
  syscall:::entry {
    this->entry = timestamp;
    @ct[probefunc, execname]  = count();
    @allct["Total", execname] = count();
  }
  syscall:::return /this->entry/ {
    this->delta = (timestamp - this->entry) / 1000;
    @time_spent[probefunc, execname]    = sum(this->delta);
    @all_time_spent["Total", execname]  = sum(this->delta);
    this->delta = 0;
    this->entry = 0;
  }
  tick-10sec {
    /* Average out measurements collected over 10 second interval */
    normalize(@time_spent, 10);
    normalize(@ct, 10);
    normalize(@all_time_spent, 10);
    normalize(@allct, 10);
    printa("function=%s execn=%s count per second=%@d | spent per second=%@d(uS)\n",
      @ct, @time_spent);
    printf("---- Summary ----\n");
    printa("%s execn=%s count per second=%@d | spent per second=%@d(uS)\n",
      @allct, @all_time_spent);
    exit(0);
  }' -p `pgrep drivedetectiond`


// Focus specifically on executable named drivedetectiond
dtrace -qn '
  syscall:::entry /execname=="drivedetectiond"/ {
    this->entry = timestamp;
    @ct[probefunc]  = count();
    @allct["Total"] = count();
  }
  syscall:::return /this->entry/ {
    this->delta = (timestamp - this->entry) / 1000;
    @time_spent[probefunc]    = sum(this->delta);
    @all_time_spent["Total"]  = sum(this->delta);
    this->delta = 0;
    this->entry = 0;
  }
  tick-10sec {
    /* Average out measurements collected over 10 second interval */
    normalize(@time_spent, 10);
    normalize(@ct, 10);
    normalize(@all_time_spent, 10);
    normalize(@allct, 10);
    printa("function=%s count per second=%@d | spent per second=%@d(uS)\n",
      @ct, @time_spent);
    printf("---- Summary ----\n");
    printa("%s count per second=%@d | spent per second=%@d(uS)\n",
      @allct, @all_time_spent);
    exit(0);
  }'