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.

34 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. สมัคร ufabet

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

    ReplyDelete
  5. 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
  6. 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
  7. It is the one money related capacity of any business that must be reliable and precise. tax preparation

    ReplyDelete
  8. 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
  9. 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
  10. The last enormous Jolt that will hold your business plan together is CARE.Bluehost review

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

    ReplyDelete
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. What is an outstanding post! “I’ll be back” (to read more of your content). Thanks for the nudge! here

    ReplyDelete
  22. I know your expertise on this. I must say we should have an online discussion on this. Writing only comments will close the discussion straight away! And will restrict the benefits from this information. 먹튀검증

    ReplyDelete
  23. Nice to be visiting your blog once more, it has been months for me. Well this article that ive been waited for therefore long. i want this article to finish my assignment within the faculty, and it has same topic together with your article. Thanks, nice share. pii-email

    ReplyDelete
  24. Thank you so much for such a well-written article. It’s full of insightful information. Your point of view is the best among many without fail.For certain, It is one of the best blogs in my opinion. 토토커뮤니티

    ReplyDelete
  25. Nice to be visiting your blog once more, it has been months for me. Well this article that ive been waited for therefore long. i want this article to finish my assignment within the faculty, and it has same topic together with your article. Thanks, nice share. 안전놀이터

    ReplyDelete
  26. 123dic เข้าสู่ระบบ Thanks for the great article. 123dic เข้าสู่ระบบ

    ReplyDelete
  27. When your website or blog goes live for the first time, it is exciting. That is until you realize no one but you and your. playist

    ReplyDelete
  28. This is my first visit to your web journal! We are a group of volunteers and new activities in the same specialty. Website gave us helpful data to work. welders

    ReplyDelete
  29. Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. crush

    ReplyDelete
  30. Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. songs

    ReplyDelete
  31. I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! playist

    ReplyDelete
  32. 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. music

    ReplyDelete
  33. Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. unique domain

    ReplyDelete
  34. Your website is really cool and this is a great inspiring article. Thank you so much. rolet online

    ReplyDelete