Thursday, February 5, 2015

Digging into microstate accounting

Solaris has support for microstate accounting. This gives huge insight into where an application and its threads are spending their time. It breaks down time into the (obvious) user and system, but also allows you to see the time spent waiting on page faults and other useful-to-know states.

This level of detail is available through the usage file in /proc/pid, there's a corresponding file for each lwp in /proc/pid/lwp/lwpid/lwpusage. You can find more details about the /proc file system in documentation, or reading my recent article about tracking memory use.

Here's an example of using it to report idle time, ie time when the process wasn't busy:

#include <stdio.h>
#include <sys/resource.h>
#include <unistd.h>
#include <fcntl.h>
#include <procfs.h>

void busy()
{
  for (int i=0; i<100000; i++)
  {
   double d = i;
   while (d>0) { d=d *0.5; }
  }
}

void lazy()
{
  sleep(10);
}

double convert(timestruc_t ts)
{
  return ts.tv_sec + ts.tv_nsec/1000000000.0;
}

void report_idle()
{
  prusage_t prusage;
  int fd;
  fd = open( "/proc/self/usage", O_RDONLY);
  if (fd == -1) { return; }
  read( fd, &prusage, sizeof(prusage) );
  close( fd );
  printf("Idle percent = %3.2f\n",
  100.0*(1.0 - (convert(prusage.pr_utime) + convert(prusage.pr_stime))
               /convert(prusage.pr_rtime) ) );
}

void main()
{
  report_idle();
  busy();
  report_idle();
  lazy();
  report_idle();
}

The code has two functions that take time. The first does some redundant FP computation (that cannot be optimised out unless you tell the compiler to do FP optimisations), this part of the code is CPU bound. When run the program reports low idle time for this section of the code. The second routine calls sleep(), so the program is idle at this point waiting for the sleep time to expire, hence this section is reported as being high idle time.

26 comments:

  1. Keep it up!! You have done the nice job having provided the latest information.

    Accountants in Toronto

    ReplyDelete
  2. Thanks for picking out the time to discuss this, I feel great about it and love studying more on this topic. It is extremely helpful for me. Thanks for such a valuable help again. Accounting System

    ReplyDelete
  3. After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article. สำนักงานบัญชีสมุทรปราการ

    ReplyDelete
  4. After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article. สมัคร ufabet

    ReplyDelete
  5. I need perusing and I consider this site got some genuinely helpful stuff on it!Quickbooks Technical Support

    ReplyDelete
  6. January is the start of duty season, and alongside it comes the start of income from annual assessment arrangement. The expansion in income will come directly at the time the accountants need it the extremely most.cheap accounting services

    ReplyDelete
  7. The following significant idea is to welcome that there are two particular, all-encompassing sorts of records that we can use in our sets or books of records.outsource payroll singapore

    ReplyDelete
  8. It is the one money related capacity of any business that must be reliable and precise. tax preparation

    ReplyDelete
  9. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. https://cbalance.hu/

    ReplyDelete
  10. This was a really great contest and hopefully I can attend the next one. It was alot of fun and I really enjoyed myself.. taxfyle.com

    ReplyDelete
  11. The last enormous Jolt that will hold your business plan together is CARE.Bluehost review

    ReplyDelete
  12. Love to read it,Waiting For More new Update and I Already Read your Recent Post its Great Thanks. www.taxfyle.com

    ReplyDelete
  13. I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well. crewe taxi Service

    ReplyDelete
  14. Very nice article, I enjoyed reading your post, very nice share, I want to twit this to my followers. Thanks!. taxfyle.com/small-business-tax-calculator

    ReplyDelete
  15. Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.. sales tax calculator

    ReplyDelete
  16. Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it. CIDB Registration

    ReplyDelete
  17. I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. click here

    ReplyDelete
  18. It is great to have the opportunity to read a good quality article with useful information on topics that plenty are interested one.I concur with your conclusions and will eagerly look forward to your future updates. where to buy ellovi skincare products

    ReplyDelete
  19. Once I thought about things like: why such information is for free here? Because when you write a book then at least on selling a book you get a percentage. Thank you and good luck on informing people more about it… Crystals

    ReplyDelete
  20. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck. Brian Gets Cars

    ReplyDelete
  21. They are prepared to manage true business and do have the adequate ability to look beyond(behind) the numbers. The extent of the legal accountants are developing at a quick movement. London accountant

    ReplyDelete
  22. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. testbank

    ReplyDelete
  23. Nice post. I was checking constantly this blog and I’m impressed! Extremely useful info specially the last part I care for such information a lot. I was seeking this certain info for a long time. Thank you and good luck. 먹튀검증

    ReplyDelete
  24. Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it. 먹튀검증

    ReplyDelete
  25. What is an outstanding post! “I’ll be back” (to read more of your content). Thanks for the nudge! here

    ReplyDelete