home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3523 < prev    next >
Encoding:
Internet Message Format  |  1991-06-23  |  15.2 KB

  1. From: billd@fps.com (George Baily)
  2. Newsgroups: comp.protocols.time.ntp,alt.sources
  3. Subject: Tools for people using software clocks only with xntp.
  4. Message-ID: <18633@celit.fps.com>
  5. Date: 22 Jun 91 02:03:35 GMT
  6.  
  7. This is an updated version of my tools for people using software only
  8. clocks with xntp.  Against my better judgement, I'm cross-posting to
  9. alt.sources thinking that maybe some people who might like these might
  10. not read comp.protocols.time.ntp.  Probably not but who knows?
  11.  
  12. If the next paragraph makes no sense to you, go on to the README below.
  13.  
  14. In any case, I've made a few improvements on these fairly trivial
  15. programs.  tclock is quite a lot more efficient in terms of the use of
  16. system resources.  I got rid of a lot of unneccessary system calls.
  17. The default output format is different but 4 formats are now available
  18. via command line switches.  adjtime can now optionally wait for the
  19. adjustment to finish and it prints out the pending adjustment once
  20. per second while it waits.  doadj makes use of this and also now
  21. puts the old value of tickadj back rather than wimping out and doing
  22. a tickadj -A.
  23.  
  24. I made these things for myself and didn't really intend them for
  25. distribution so they are fairly hacked looking.  I did try to clean
  26. them up a little more this time but they are still fairly rough.
  27. Fortunately, they are trivial enough that it doesn't really matter
  28. that much.
  29.  
  30. Enjoy.
  31.  
  32. ** README ********************************************************
  33.  
  34. These programs are crude hacks but they make working with a software
  35. reference clock with no good hardware reference clock available a
  36. little easier.
  37.  
  38. I try to synchronize my time with the phone company time recording.  To
  39. do this, I first run "tclock" which stands for "text clock" which
  40. prints out the current time constantly in one of the following forms:
  41. -h: "\rHH:MM:SS.FF", -m "\rMM:SS.FF", -s "\rSS.FF", or -d (default)
  42. "\rSS.F".  I currently have it set to print out every 0.1 seconds.  I
  43. figure I can see the difference between times that are about .1 or .2
  44. off from each other so this is about as good as it gets when doing this
  45. stuff by hand.  Anyway, I call the "time" number and watch the running
  46. tclock and try to guess how far off it is from the phone company time.
  47.  
  48. After making a guess I then want to adjust it.  "date(1)" is a pain.
  49. There is a variable delay between the time you hit return and the time
  50. that the clock is actually set.  So I've written a program called
  51. "adjtime" which is simply a shell level access program to the
  52. adjtime(2) system call.  It takes as it's parameter a single floating
  53. point number of the amount of seconds to adjust by.  If you have your
  54. tickadj variable set to a small value (as ntp documentation always
  55. recomends), it will take a hell of a long time to make up differences
  56. large enough for humans to distinguish.  For that reason, I've made a
  57. script called "doadj" which does a tickadj to make tickadj 10% of tick
  58. (normal default) while it runs adjtime.  It then waits an appropriate
  59. amount of time for the tickadj to finish and runs "tickadj -a" to set
  60. things back to normal.  You may not want to have (x)ntpd running when
  61. you do this since it might do it's own adjtime and wipe you out.  If
  62. you leave it running and the time doesn't get adjusted, that's probably
  63. what happened.  Just do it again.  Adjtime can optionally take a "-w"
  64. flag which causes it to wait for the comepensation to finish before it
  65. exits and print out how much compensation is left once per second.
  66. This is a pacifier that I find I need.
  67.  
  68. READ AND UNDERSTAND THE doadj SCRIPT BEFORE YOU RUN IT!
  69. It may need modification for your system.
  70.  
  71. Now you can run tclock again and call the "time" number again to see
  72. how close you are.  You may need to adjust again.
  73.  
  74. If you keep track of how much you have to adjust over given time
  75. periods you can guess a good value for the drift compensation
  76. register.  If you are using the software pseudo-clock from xntp, you
  77. can use this value for your fudge time1 value.  You can refine it
  78. over longer and longer test periods as you keep using it.
  79.  
  80. This stuff was written on and is used on BSD systems.  I don't know if
  81. tclock will run on System V since I don't know if System V has
  82. setitimer(2) or some of the signal stuff.  I don't care to port it and
  83. I won't support it.  You're on your own.
  84.  
  85. Usual disclaimers: Run this stuff at your own risk.  Neither I nor my
  86. company are willing to take repsonsibility for this and in fact this
  87. whole posting could be a forgery made it look like it came from
  88. billd@fps.com so you'll have a hell of a time proving that I posted
  89. it.  So there.
  90.  
  91. --Bill Davidson billd@fps.com 6/21/91
  92.  
  93. #! /bin/sh
  94. # This is a shell archive.  Remove anything before this line, then unpack
  95. # it by saving it into a file and typing "sh file".  To overwrite existing
  96. # files, type "sh file -c".  You can also feed this as standard input via
  97. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  98. # will see the following message at the end:
  99. #        "End of shell archive."
  100. # Contents:  README Makefile adjtime.c tclock.c doadj.sh
  101. # Wrapped by billd@celit on Mon May 20 20:26:18 1991
  102. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  103. if test -f 'README' -a "${1}" != "-c" ; then 
  104.   echo shar: Will not clobber existing file \"'README'\"
  105. else
  106. echo shar: Extracting \"'README'\" \(2559 characters\)
  107. sed "s/^X//" >'README' <<'END_OF_FILE'
  108. X
  109. XThese programs are crude hacks but they make working with a software
  110. Xreference clock with no good hardware reference clock available a
  111. Xlittle easier.
  112. X
  113. XI try to synchronize my time with the phone company time recording.  To
  114. Xdo this, I first run "tclock" which stands for "text clock" which
  115. Xprints out the current time constantly in the form "\rHH:MM:SS.FF".  I
  116. Xcurrently have it set to print out every 0.1 seconds.  I figure I can
  117. Xsee the difference between times that are about .2 off from each other
  118. Xso this is about as good as it gets when doing this stuff by hand.
  119. XAnyway, I call the "time" number and watch the running tclock and try
  120. Xto guess how far off it is from the phone company time.
  121. X
  122. XAfter making a guess I then want to adjust it.  "date(1)" is a pain.
  123. XThere is a variable delay between the time you hit return and the time
  124. Xthat the clock is actually set.  So I've written a program called
  125. X"adjtime" which is simply a shell level access program to the
  126. Xadjtime(2) system call.  It takes as it's parameter a single floating
  127. Xpoint number of the amount of seconds to adjust by.  If you have your
  128. Xtickadj variable set to a small value (as ntp documentation always
  129. Xrecomends), it will take a hell of a long time to make up differences
  130. Xlarge enough for humans to distinguish.  For that reason, I've made a
  131. Xscript called "doadj" which does a tickadj to make tickadj 10% of tick
  132. X(normal default) while it runs adjtime.  It then waits an appropriate
  133. Xamount of time for the tickadj to finish and runs "tickadj -A" to set
  134. Xthings back to normal.  You may not want to have (x)ntpd running when
  135. Xyou do this since it might do it's own adjtime and wipe you out.  If
  136. Xyou leave it running and the time doesn't get adjusted, that's probably
  137. Xwhat happened.  Just do it again.
  138. X
  139. XREAD AND UNDERSTAND THE doadj SCRIPT BEFORE YOU RUN IT!
  140. XIt may need modification for your system.
  141. X
  142. XNow you can run tclock again and call the "time" number again to see
  143. Xhow close you are.  You may need to adjust again.
  144. X
  145. XThis stuff was written on and is used on BSD systems.  I don't know if
  146. Xtclock will run on System V since I don't know if System V has
  147. Xsetitimer(2) or some of the signal stuff.  I don't care to port it and
  148. XI won't support it.  You're on your own.
  149. X
  150. XUsual disclaimers: Run this stuff at your own risk.  Neither I nor my
  151. Xcompany are willing to take repsonsibility for this and in fact this
  152. Xwhole posting could be a forgery made it look like it came from
  153. Xbilld@fps.com so you'll have a hell of a time proving that I posted
  154. Xit.  So there.
  155. X
  156. X--Bill Davidson billd@fps.com 5/20/91
  157. END_OF_FILE
  158. if test 2559 -ne `wc -c <'README'`; then
  159.     echo shar: \"'README'\" unpacked with wrong size!
  160. fi
  161. # end of 'README'
  162. fi
  163. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  164.   echo shar: Will not clobber existing file \"'Makefile'\"
  165. else
  166. echo shar: Extracting \"'Makefile'\" \(368 characters\)
  167. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  168. X#
  169. X# quick hack Makefile
  170. X#
  171. X
  172. XCFLAGS=        -O
  173. XPROGRAMS=    adjtime tclock doadj
  174. XFILES=        README Makefile adjtime.c tclock.c doadj.sh
  175. X
  176. Xall: $(PROGRAMS)
  177. X
  178. Xadjtime: adjtime.o
  179. X    $(CC) $(CFLAGS) -o $@ $@.o
  180. X
  181. Xtclock: tclock.o
  182. X    $(CC) $(CFLAGS) -o $@ $@.o
  183. X
  184. Xdoadj: doadj.sh
  185. X    cp $@.sh $@
  186. X    chmod +x $@
  187. X
  188. Xclean:
  189. X    rm -f $(PROGRAMS) *.o
  190. X
  191. Xsoftclock.shar: $(FILES)
  192. X    shar -o softclock.shar $(FILES)
  193. X
  194. END_OF_FILE
  195. if test 368 -ne `wc -c <'Makefile'`; then
  196.     echo shar: \"'Makefile'\" unpacked with wrong size!
  197. fi
  198. # end of 'Makefile'
  199. fi
  200. if test -f 'adjtime.c' -a "${1}" != "-c" ; then 
  201.   echo shar: Will not clobber existing file \"'adjtime.c'\"
  202. else
  203. echo shar: Extracting \"'adjtime.c'\" \(1799 characters\)
  204. sed "s/^X//" >'adjtime.c' <<'END_OF_FILE'
  205. X/******************************************************************************
  206. X*
  207. X*    ADJTIME - Apply the adjtime call with the value on the command
  208. X*    line.  Reports value of pending adjtime which you just wiped
  209. X*    out (see adjtime(2)).  Takes only one parameter: a floating
  210. X*    point number which is the number of seconds to adjust by.
  211. X*    Positive numbers speed up the clock, negative numbers slow
  212. X*    it down.
  213. X*
  214. X*    This program must be run by root to have any effect.
  215. X*
  216. X*    You probably want to have tickadj set to a large value, say
  217. X*    tick/10 (normal value).  If you have a small value for ntp,
  218. X*    you may want to temporarily run "tickadj -a 1000" before
  219. X*    running adjtime and "tickadj -A" after running adjtime and
  220. X*    after the clock has come into the value you want.
  221. X*
  222. X*    This program makes it a little easier to get the system clock
  223. X*    set to the proper time in the face of delays running date(1).
  224. X*
  225. X******************************************************************************/
  226. X
  227. X#include <stdio.h>
  228. X#include <ctype.h>
  229. X#include <sys/time.h>
  230. X
  231. Xdouble atof();
  232. Xvoid doadjtime();
  233. X
  234. X#define issign(x)    (((x)=='-')||((x)=='+'))
  235. X
  236. Xvoid main( argc, argv )
  237. Xint    argc;
  238. Xchar    *argv[];
  239. X{
  240. X    double diff;
  241. X    char *val;
  242. X
  243. X    if ( argc < 2 )
  244. X    usage();
  245. X    val = argv[1];
  246. X    if ( isdigit(val[0]) || ( isdigit(val[1]) && issign(val[0]) ) ){
  247. X    diff = atof(argv[1]);
  248. X    doadjtime(diff);
  249. X    }else
  250. X    usage();
  251. X}
  252. X
  253. Xint usage()
  254. X{
  255. X    fprintf(stderr,"usage: adjtime seconds\n");
  256. X    exit(-1);
  257. X}
  258. X
  259. Xvoid doadjtime(diff)
  260. Xdouble    diff;
  261. X{
  262. X    struct timeval tp, otp;
  263. X
  264. X    tp.tv_sec = (long)diff;
  265. X    tp.tv_usec = (long)(1000000 * (diff - tp.tv_sec));
  266. X    printf("adjusting %ld seconds, %ld useconds\n",tp.tv_sec,tp.tv_usec);
  267. X    if ( adjtime( &tp, &otp ) ){
  268. X    perror("adjtime");
  269. X    exit(-1);
  270. X    }else
  271. X    printf("%ld %ld\n",otp.tv_sec,otp.tv_usec);
  272. X}
  273. X
  274. END_OF_FILE
  275. if test 1799 -ne `wc -c <'adjtime.c'`; then
  276.     echo shar: \"'adjtime.c'\" unpacked with wrong size!
  277. fi
  278. # end of 'adjtime.c'
  279. fi
  280. if test -f 'tclock.c' -a "${1}" != "-c" ; then 
  281.   echo shar: Will not clobber existing file \"'tclock.c'\"
  282. else
  283. echo shar: Extracting \"'tclock.c'\" \(2924 characters\)
  284. sed "s/^X//" >'tclock.c' <<'END_OF_FILE'
  285. X/******************************************************************************
  286. X*
  287. X*    TCLOCK - This program prints a text clock.  It tries to do it
  288. X*    ten times per second.  It prints out accurate to 1/100th of
  289. X*    a second.  Quit with SIGINT (usually control-C).
  290. X*
  291. X*    Needed Library Routines:
  292. X*
  293. X*        gettimeofday(2)
  294. X*        setitimer(2)
  295. X*        sigpause(2)
  296. X*        sigsetmask(2)
  297. X*
  298. X*        exit(3)
  299. X*        fflush(3)
  300. X*        localtime(3)
  301. X*        printf(3)
  302. X*        signal(3)
  303. X*
  304. X******************************************************************************/
  305. X
  306. X#include <stdio.h>
  307. X#include <sys/time.h>
  308. X#include <sys/signal.h>
  309. X
  310. X/* set FREQ to the number of microseconds to sleep between printouts
  311. X   I try to get it so that it prints out every 0.1 seconds.  Due to
  312. X   program overhead and resource sharing, this is usually a bit less
  313. X   than 100000 microseconds (=0.1 seconds).  If you get it right, the
  314. X   hundredths place will stay constant. */
  315. X
  316. X#define    FREQ    99990
  317. X
  318. Xvoid fsleep();
  319. X
  320. X/* cheap signal handler */
  321. Xint
  322. Xhandler()
  323. X{
  324. X    printf("\n");
  325. X    exit(0);
  326. X}
  327. X
  328. X/******************************************************************************
  329. X*
  330. X*    Main program.
  331. X*        Just an infinite loop getting and printing out the time.
  332. X*        Sets up a signal handler to abort on SIGINT or SIGTERM.
  333. X*
  334. X*    Library routines:
  335. X*        gettimeofday(2)
  336. X*        fflush(3)
  337. X*        localtime(3)
  338. X*        printf(3)
  339. X*        signal(3)
  340. X*
  341. X******************************************************************************/
  342. Xvoid
  343. Xmain()
  344. X{
  345. X    struct tm *tp;
  346. X    struct timeval tv;
  347. X    struct timezone tz;
  348. X
  349. X    signal( SIGINT, handler );
  350. X    signal( SIGTERM, handler );
  351. X    while ( 1 ){
  352. X    gettimeofday( &tv, &tz );
  353. X    tp = localtime(&tv.tv_sec);
  354. X    printf("\r%02d:%02d:%02d.%02d", tp->tm_hour, tp->tm_min, tp->tm_sec,
  355. X                            tv.tv_usec/10000 );
  356. X    fflush(stdout);
  357. X    fsleep( (unsigned long)0, (unsigned long)FREQ );
  358. X    }
  359. X}
  360. X
  361. Xstatic void
  362. Xfsleeptrap()
  363. X{
  364. X    /* Zen function */
  365. X}
  366. X
  367. X/******************************************************************************
  368. X*
  369. X*    FSLEEP - Fine sleep function; sleeps at resolution of the
  370. X*    interval timer (see setitimer(2)) or microseconds; whichever
  371. X*    is worse.
  372. X*
  373. X*    Library routines:
  374. X*        setitimer(2)
  375. X*        sigpause(2)
  376. X*        sigsetmask(2)
  377. X*        signal(3)
  378. X*
  379. X******************************************************************************/
  380. Xvoid
  381. Xfsleep( secs, usecs )
  382. Xunsigned long secs, usecs;
  383. X{
  384. X    int  omask, (*osig)();
  385. X    struct itimerval tval[2];
  386. X
  387. X    /* setup */
  388. X    osig = signal( SIGALRM, fsleeptrap );
  389. X
  390. X    if ( usecs >= 1000000L ){
  391. X    secs += usecs / 1000000L;
  392. X    usecs %= 1000000L;
  393. X    }
  394. X    tval[1].it_interval.tv_sec = 0L;
  395. X    tval[1].it_interval.tv_usec = 0L;
  396. X    tval[1].it_value.tv_sec = secs;
  397. X    tval[1].it_value.tv_usec = usecs;
  398. X
  399. X    /* more setup */
  400. X    omask = sigsetmask( ( 1 << (SIGALRM-1) ) );
  401. X    setitimer( ITIMER_REAL, &tval[1], &tval[0] );
  402. X
  403. X    /* wait for interrupt */
  404. X    sigpause( 0 );
  405. X
  406. X    /* cleanup */
  407. X    signal( SIGALRM, osig );
  408. X    setitimer( ITIMER_REAL, &tval[0], &tval[1] );
  409. X    sigsetmask( omask );
  410. X
  411. X    return;
  412. X}
  413. END_OF_FILE
  414. if test 2924 -ne `wc -c <'tclock.c'`; then
  415.     echo shar: \"'tclock.c'\" unpacked with wrong size!
  416. fi
  417. # end of 'tclock.c'
  418. fi
  419. if test -f 'doadj.sh' -a "${1}" != "-c" ; then 
  420.   echo shar: Will not clobber existing file \"'doadj.sh'\"
  421. else
  422. echo shar: Extracting \"'doadj.sh'\" \(698 characters\)
  423. sed "s/^X//" >'doadj.sh' <<'END_OF_FILE'
  424. X#!/bin/sh -x
  425. X#
  426. X# This script temporarily changes tickadj and runs adjtime to fix your
  427. X# system time.  It's only parameter is the amount of time to adjust by
  428. X# which is passed to adjtime.
  429. X#
  430. X# set path so that you can find tickadj and adjtime
  431. X#
  432. XPATH="$PATH:/usr/local/etc"
  433. X#
  434. X# find tick/10 so you can make large adjustments with adjtime.
  435. X#
  436. Xticka=`echo tick/D |adb -k /vmunix /dev/mem |grep tick |awk '{printf("%d\n",$2/10)}'`
  437. X#
  438. X# set tickadj to a large value and do the adjtime.
  439. X#
  440. Xtickadj -a $ticka
  441. Xadjtime $1
  442. X#
  443. X# sleep long enough for the adjtime to finish.
  444. X#
  445. Xtim=`echo $1 | awk '{s=$1;if(s<0){s=-1*s};printf("%d\n",((10*s)+0.5))}'`
  446. Xsleep $tim
  447. X#
  448. X# Restore tickadj to it's proper value.
  449. X#
  450. Xtickadj -A
  451. END_OF_FILE
  452. if test 698 -ne `wc -c <'doadj.sh'`; then
  453.     echo shar: \"'doadj.sh'\" unpacked with wrong size!
  454. fi
  455. # end of 'doadj.sh'
  456. fi
  457. echo shar: End of shell archive.
  458. exit 0
  459.