> Actually, gethrtime is in section 3C: > > "(3C) These functions, together with those of Section 2, > constitute the standard C library" > > And indeed, if we do some further digging: > > http://docs.hp.com/en/B2355-60130/gethrtime.3C.html (HP-UX 11iv3) > > So, this says more about Linux than about your coding.
Well, one should be able to just compile something simple and expect it to work but often times the -Xc switch can stop you in your tracks. Consider this itty bitty thing : #include <stdio.h> #include <sys/time.h> hrtime_t start_hrt, end_hrt; int main(int argc, char *argv[]) { start_hrt = gethrtime(); printf ( "Hello World!\n" ); end_hrt = gethrtime(); printf("time = %lld nsec\n", ( end_hrt - start_hrt ) ); return (0); } Pretty simple and you can compile it with Studio 8 ( or whatever ) thus : first .. the old old compiler : $ cc -V cc: Sun C 5.5 Patch 112760-18 2005/06/14 usage: cc [ options] files. Use 'cc -flags' for details the compiler switches : $ cc -xstrconst -xildoff -xarch=generic -H -xtime -Xa -c -o hello.o hello.c /usr/include/stdio.h /usr/include/iso/stdio_iso.h /usr/include/sys/feature_tests.h /usr/include/sys/isa_defs.h /usr/include/sys/va_list.h /usr/include/stdio_tag.h /usr/include/stdio_impl.h /usr/include/sys/time.h /usr/include/sys/types.h /usr/include/sys/machtypes.h /usr/include/sys/int_types.h /usr/include/sys/select.h /usr/include/sys/time.h /usr/include/time.h /usr/include/iso/time_iso.h /usr/include/sys/time_impl.h acomp real 0.8 user 0.3 sys 0.2 I like to see all the header files being traversed. For something so simple, amazing isn't it? $ cc -xarch=generic -o hello hello.o $ file hello hello: ELF 32-bit MSB executable SPARC Version 1, dynamically linked, not stripped $ ./hello Hello World! time = 3765000 nsec $ /bin/ptime ./hello Hello World! time = 4000000 nsec real 0.055 user 0.010 sys 0.051 $ So that works as expected until you go to -Xc for strict compliance with some pedantic standards. $ cc -xstrconst -xildoff -xarch=generic -H -xtime -Xc -\# -c -o hello.o hello.c ### Note: NLSPATH = /opt/SUNWspro/prod/bin/../lib/locale/%L/LC_MESSAGES/%N.cat:/opt/SUNWspro/prod/bin/../../lib/locale/%L/LC_MESSAGES/%N.cat ### command line files and options (expanded): ### -c -H -Xc -xarch=generic -xildoff -xstrconst -xtime hello.c -o hello.o /opt/SUNWspro/prod/bin/acomp -xldscope=global -i hello.c -y-fbe -y/opt/SUNWspro/prod/bin/fbe -y-xarch=generic -y-o -yhello.o -y-verbose -y-xmemalign=4s -y-xthreadvar=no%dynamic -y-comdat -xdbggen=no%dwarf2+stabs -strconst -H -fsimple=0 -xarch=generic -fparam_ir -Qy -D__SunOS_5_8 -D__SUNPRO_C=0x550 -D__SVR4 -D__unix -D__sun -D__sparc -D__BUILTIN_VA_ARG_INCR -xc99=%all,no%lib -Xc -D__PRAGMA_REDEFINE_EXTNAME -I/opt/SUNWspro/prod/include/cc "-g/opt/SUNWspro/prod/bin/cc -xstrconst -xildoff -xarch=generic -H -xtime -Xc -c -o hello.o " -D__SUN_PREFETCH -destination_ir=yabe /usr/include/stdio.h /usr/include/iso/stdio_iso.h /usr/include/sys/feature_tests.h /usr/include/sys/isa_defs.h /usr/include/sys/va_list.h /usr/include/stdio_tag.h /usr/include/stdio_impl.h /usr/include/sys/time.h /usr/include/sys/types.h /usr/include/sys/machtypes.h /usr/include/sys/int_types.h /usr/include/sys/select.h /usr/include/sys/time.h /usr/include/time.h /usr/include/iso/time_iso.h "hello.c", line 12: operands have incompatible types: union {double _d, array[2] of int _l} "-" union {double _d, array[2] of int _l} cc: acomp failed for hello.c Note all the things defined with -D and then an underscore. I think they are all covered off in the Sun Studio 8: C User's Guide but in most cases I would use Studio 11. Today I am using Studio 8 for reasons to be revealed eventually. But I digress. Let's look at what went wrong there. It looks to me that we have some funky datatype which is really a union. That would be this thing : /* * Time expressed as a 64-bit nanosecond counter. */ typedef longlong_t hrtime_t; I found that by tracking down the underlying definition for hrtime_t which counts our nanoseconds ( supposedly ) : $ grep hrtime_t /usr/include/sys/time.h typedef longlong_t hrtime_t; extern hrtime_t gethrtime(void); extern hrtime_t gethrtime_unscaled(void); extern hrtime_t gethrtime_max(void); extern void scalehrtime(hrtime_t *); extern void hrt2ts(hrtime_t, timestruc_t *); extern hrtime_t ts2hrt(timestruc_t *); extern void hrt2tv(hrtime_t, struct timeval *); extern hrtime_t tv2hrt(struct timeval *); extern void hrt2ts32(hrtime_t, timestruc32_t *); hrtime_t gethrtime(void); hrtime_t gethrvtime(void); hrtime_t gethrtime(); hrtime_t gethrvtime(); A bit of poking around finds this in /usr/include/sys/time.h /* * Needed for longlong_t type. Placement of this due to <sys/types.h> * including <sys/select.h> which relies on the presense of the itimerval * structure. */ and eventually this : typedef long long longlong_t; is in /usr/include/sys/types.h so then why don't we just use that and be done with it ? Why the union ? Does not matter much because the data being returned by gethrtime(3C) is not a long long and does need to be some funky union : "hello.c", line 11: assignment type mismatch: long long "=" union {double _d, array[2] of int _l} "hello.c", line 13: assignment type mismatch: long long "=" union {double _d, array[2] of int _l} cc: acomp failed for hello.c attempts to get around that ... fail. Is this portable and part of some standard C library? I don't see how really. But it compiles nicely on Solaris with -Xa and maybe even a transitional switch but forget pedantic or strict compliance with some ANSI standard unless you are willing to dig around a LOT to solve the data type problem. I'd love to hear suggestions *because* I do have code that I'd like to see run on both Solaris and Linux which reports fine grained timing information with some degree of accuracy. Dennis _______________________________________________ opensolaris-discuss mailing list opensolaris-discuss@opensolaris.org