home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / programm / 3868 < prev    next >
Encoding:
Internet Message Format  |  1992-07-23  |  5.1 KB

  1. Xref: sparky comp.unix.programmer:3868 comp.unix.internals:1595
  2. Path: sparky!uunet!gatech!mailer.cc.fsu.edu!sun13!pi!mueller
  3. From: mueller@pi.fsu.edu (Frank Mueller)
  4. Newsgroups: comp.unix.programmer,comp.unix.internals
  5. Subject: ANSWERS How to measure context switches
  6. Message-ID: <9939@sun13.scri.fsu.edu>
  7. Date: 23 Jul 92 16:54:53 GMT
  8. References: <AJG.92Jul22114544@sp1.ccd.harris.com> <1992Jul23.144652.1785@b11.b11.ingr.com>
  9. Sender: news@sun13.scri.fsu.edu
  10. Followup-To: comp.unix.programmer
  11. Organization: Florida State University Computer Science
  12. Lines: 129
  13.  
  14. Not too long ago I posted the following question, summarized below are
  15. some responses and I comment on my part about my experience with the
  16. different solutions. Thanks for those of you who responded.
  17.  
  18. Original posting:
  19. > Hi there!
  20. > I'm trying to measure the context switch overhead of UNIX processes (SunOS 4.1).
  21. > I did something like this:
  22. > fork off a child,
  23. > lower the parent's priority (-> nice lavel)
  24. > child installs a signal handler
  25. > child suspends on sigpause
  26. > parent reads clock (before)
  27. > parent sends signal to child
  28. > child resumes
  29. > child executes signal handler which contains a call to read the clock (after)
  30. > The approximate time for the context switch is (after-before). But I am also
  31. > timing some operations which are not part of the context switch:
  32. > (a) the parent's sending of the signal
  33. > (b) the overhead of installing a signal handler for the child (_sigtramp etc.)
  34. > Does anyone know a more accurate way to measure the context switch time
  35. > between UNIX processes?
  36. > -------------------------------------------------------------------------
  37. Answer:
  38. > hello,
  39. >     I don't know how to accurately do the timings outside the kernel
  40. > I'm afraid. However I don't see how your method can work given the 
  41. > accuracy of the clock in Unix. In Unix accuracy is usually only as fine
  42. > grained as the system HZ rate, i.e. about 10-20 milliseconds, which is
  43. > far longer than context switch time. You are also measuring (above) the
  44. > time for the clock system call. You really need to get into the kernel..
  45. > I'd be interested if anyone thinks up something sneaky.
  46. > warwick
  47. > ps. Is this thread package going to be public domain? Do you know of
  48. > any that are? Thanks.
  49. My comment:
  50.  
  51. This is very true, the ``accuracy'' of the UNIX timer doesn't allow
  52. for measurements at such a low level. My original post left out a few
  53. (but spicy) details about my approach: I use the dual-loop timing
  54. method: My first measurement executes an empty counter loop for n
  55. times and measures the time1 spent in this loop. The clock is read
  56. before and after the loop. The second loop contains something like the
  57. code described above but I have two tasks signalling each other,
  58. thereby causing two context switches per iteration. Time2 is the time
  59. spent in this second loop. Therefore, the average context switch time
  60. is
  61.  
  62. (time2 - time1) / number of iterations.
  63.  
  64. As you can see, the overhead of reading the clock can be neglected.
  65. But the overhead of signalling for sender and receiver is also
  66. included.
  67.  
  68. Here are the results for approach1:
  69. 10000 process context switches
  70. 4134.49 milliseconds elapsed time
  71. 413.45 microseconds per operation
  72.  
  73. > -------------------------------------------------------------------------
  74. Answer:
  75. > Frank,
  76. > > Does anyone know a more accurate way to measure the context switch time
  77. > > between UNIX processes?
  78. > I have done a similar exp. earlier and used IPC semaphore to cause context
  79. > switches between two processes.  If you can run two processes that keeps
  80. > releasing and acquiring a semaphore in an idle system and measure the
  81. > number of switches / sec. (sar -p will give that) you can get a better
  82. > estimate of context switch overhead.
  83. > Inform me if you get any better suggestion.
  84. > Subbu
  85. > HP/Cupertino
  86.  
  87. I implemented this also with the dual loop timing method. The body of
  88. the second loop contains calls to lock/unlock semaphores only.
  89.  
  90. Here are the results for approach2:
  91. 10000 process context switches
  92. 3868.02 milliseconds elapsed time
  93. 386.80 microseconds per operation
  94.  
  95. All measurements were taken on a SUN IPC.
  96.  
  97. This seems to indicate that the overhead of signals is somewhat lower
  98. than semaphores.
  99.  
  100. Then I wrote a dual loop timing where a process installed a signal
  101. handler and send the signal repeatedly to itself. This way I timed the
  102. overhead of executing a signal handler.
  103.  
  104. Here are the results for approach3:
  105. 10000 signal overhead
  106. 2008.19 milliseconds elapsed time
  107. 200.82 microseconds per operation
  108.  
  109. Thus, the overhead for a process context switch seems to be around
  110. 210usec (approach1 - approach3).
  111.  
  112. And, to come back to Warwick's question, the context switch time for
  113. threads in my implementation is somewhere around 70usec. Maybe POSIX
  114. threads really do have a future. We are currently working on
  115. non-blocking I/O for threads. Once this is done we hope to be able to
  116. release a library version of POSIX threads for the Sun SPARC
  117. architecture under SunOS, probably in source code accessible via ftp
  118. around the end of the year!? If the different parties who are funding
  119. this project agree, that is.
  120.  
  121. Thanks for everybody's input. (And maybe someone still knows a
  122. better way...:-)
  123.  
  124. Frank
  125. mueller@cs.fsu.edu
  126.  
  127.