views:

50

answers:

1

I want a similar function similar to the VC++ GetThreadTimes function to work on solaris. I need a monitoring tool to monitor thread and monitor execution time from another thread. Is there a direct way of doing this?

I have found that getrusage() can do this only to get the value of times() for the calling thread. But I what I want to do is monitor the thread times from another thread. My last resort is to modify the implementation of CreateThread to hardwire a sig handler to the thread to be executed. And use the sighandler to grab the data for me. But I have no idea yet if this will work.

Suggestions are welcome.

TIA

+1  A: 

On Solaris you can do this by mmaping /proc/PID/lwp/LWPID/lwpstatus

Simple example:

#define _STRUCTURED_PROC 1

#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/procfs.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>

int main() {
  const time_t start = time(NULL);
  while (time(NULL) - start < 10);
  int fd = open("/proc/self/lwp/1/lwpstatus", O_RDONLY);
  assert(fd >= 0);
  lwpstatus_t status;
  const ssize_t ret = read(fd, &status, sizeof(lwpstatus_t));
  close(fd);
  assert(sizeof(lwpstatus_t) == ret);
  printf("User CPU time: %ld\n", status.pr_utime.tv_sec);
  printf("System time: %ld\n", status.pr_stime.tv_sec);
  return 0;
}

Here this is the first thread of the current process, but any values of PID and LWPID can be used provided you have sufficient access and they exist.

This ran ok on my Solairs test machine (SunOS wiked 5.10 Generic_142900-13 sun4v sparc SUNW,T5140).

I did try to use mmap for this example, but got "operation not applicable" returned from the call so gave up and used read instead.

awoodland
man -s4 proc gives more details on the information from /proc
awoodland