Performance Hz (ouch!)

I’ve used the code below to record time stamps (based on time from boot,
not time of day) and measure change of time. It seems to work fine.

Alec

#include <sys/neutrino.h>
#include <sys/syspage.h>
#include <sys/time.h>
#include <time.h>
#include <inttypes.h>
#include <pthread.h>

#define TIMEVAL_TO_SEC(tv) ( (double)(tv.tv_sec) + (
(double)(tv.tv_usec) * 1E-6 ) )

long cycpersec=0;
long timepercycle=0;

/********************************************************************

  • ivl_timer_mark():
  • This function fills the abs_time variable with the absolute
  • time since the CPU was powered (secs). This function must
  • be called before the ivl_timer_reset and ivl_timer_elapsed
  • function calls.

**********************************************************************/
void ivl_timer_mark(double *abs_time)
{
uint64_t count;

if( cycpersec == 0 )
{
cycpersec = SYSPAGE_ENTRY(qtime)->cycles_per_sec;
timepercycle = (long int)(1.0/(double)cycpersec);
}
count = ClockCycles();
*abs_time = (double)count/(double)cycpersec;
return;
}

/*****************************************************************************

  • ivl_timer_reset():
  • This function returns the elapsed time since this function or
  • ivl_timer_mark has been called (secs). It also fills the abs_time
  • variable with the absolute time since the CPU was powered.

*****************************************************************************/
double ivl_timer_reset(double *abs_time)
{
double old_time;

old_time = *abs_time;

ivl_timer_mark(abs_time);
return (*abs_time - old_time);
}

/*****************************************************************************
*

  • This function returns the elapsed time (secs) since ivl_timer_mark or
  • ivl_timer_reset has been called. It does not reset the counter or
  • change abs_time.

*****************************************************************************/
double ivl_timer_elapsed(double *abs_time)
{
double now;

ivl_timer_mark(&now);

return(now - *abs_time);
}




Sunil Kittur wrote:

Bill Caroselli wrote:

Bill Caroselli <> qtps@earthlink.net> > wrote:
BC > Colin Burgess <> cburgess@qnx.com> > wrote:
BC > CB > If you are just trying to get the difference between two
times, then use BC > CB > the SYSPAGE_ENTRY(qtime)->nsec field - this
obviates the need for a BC > CB > kernel call.

BC > I’m not looking for the difference in two times.

BC > I want to record a time stamp.
BC > Is there a convienence function to convert from
SYSPAGE_ENTRY(qtime) to
BC > a struct timespec?

BC > If not, can I assume that the conversion is simply
BC > boot_time * 1000000000000 + nsec ?

OK, I see that it is NOT that simple.

Is there a function that will do this?


nsec2timespec() converts a 64-bit nsec value to a timespec.

Do you need the timespec to be time-of-day, or just a
monotonically increasing stamp since system boot?

Sunil.

Sunil Kittur <skittur@qnx.com> wrote:
SK > Bill Caroselli wrote:

Bill Caroselli <> qtps@earthlink.net> > wrote:
BC > Colin Burgess <> cburgess@qnx.com> > wrote:
BC > CB > If you are just trying to get the difference between two times, then use
BC > CB > the SYSPAGE_ENTRY(qtime)->nsec field - this obviates the need for a
BC > CB > kernel call.

BC > I’m not looking for the difference in two times.

BC > I want to record a time stamp.

BC > Is there a convienence function to convert from SYSPAGE_ENTRY(qtime) to
BC > a struct timespec?

BC > If not, can I assume that the conversion is simply
BC > boot_time * 1000000000000 + nsec ?

OK, I see that it is NOT that simple.

Is there a function that will do this?

SK > nsec2timespec() converts a 64-bit nsec value to a timespec.

SK > Do you need the timespec to be time-of-day, or just a
SK > monotonically increasing stamp since system boot?

I’m looking for an accurate date & time.

Bill Caroselli wrote:

Sunil Kittur <> skittur@qnx.com> > wrote:
SK > Bill Caroselli wrote:

Bill Caroselli <> qtps@earthlink.net> > wrote:
BC > Colin Burgess <> cburgess@qnx.com> > wrote:
BC > CB > If you are just trying to get the difference between two times, then use
BC > CB > the SYSPAGE_ENTRY(qtime)->nsec field - this obviates the need for a
BC > CB > kernel call.

BC > I’m not looking for the difference in two times.

BC > I want to record a time stamp.

BC > Is there a convienence function to convert from SYSPAGE_ENTRY(qtime) to
BC > a struct timespec?

BC > If not, can I assume that the conversion is simply
BC > boot_time * 1000000000000 + nsec ?

OK, I see that it is NOT that simple.

Is there a function that will do this?


SK > nsec2timespec() converts a 64-bit nsec value to a timespec.

SK > Do you need the timespec to be time-of-day, or just a
SK > monotonically increasing stamp since system boot?

I’m looking for an accurate date & time.

The 6.2.1 Help says SYSPAGE_ENTRY(qtime)->nsec_tod_adjust + SYSPAGE_ENTRY(qtime)->nsec is the number
of nanoseconds since the epoch (1970). This info is under “Customizing Image Startup Programs”.
Haven’t tried this to see if it works out to the current TOD, though.

Also of note is the warning below the table about nsec & nsec_tod_adjust being a 64-bit integer that
is modified in an interrupt - a user either has to disable interrupts before reading the value(s),
or read them each twice & check that they haven’t changed. Failure to do this will probably result
in a very difficult to track down bug. :slight_smile:

Kevin N.

Kevin N <xxxx@yyyy.com> wrote:


KN > Bill Caroselli wrote:

SK > nsec2timespec() converts a 64-bit nsec value to a timespec.

SK > Do you need the timespec to be time-of-day, or just a
SK > monotonically increasing stamp since system boot?

I’m looking for an accurate date & time.

KN > The 6.2.1 Help says SYSPAGE_ENTRY(qtime)->nsec_tod_adjust + SYSPAGE_ENTRY(qtime)->nsec is the number
KN > of nanoseconds since the epoch (1970). This info is under “Customizing Image Startup Programs”.
KN > Haven’t tried this to see if it works out to the current TOD, though.

I have just tried SYSPAGE_ENTRY(qtime)->nsec. It appears to be adjusted to
apparent nsec since start of the epoc.

Is this an OS bug or does the documentation need to be updated?


KN > Also of note is the warning below the table about nsec & nsec_tod_adjust being a 64-bit integer that
KN > is modified in an interrupt - a user either has to disable interrupts before reading the value(s),
KN > or read them each twice & check that they haven’t changed. Failure to do this will probably result
KN > in a very difficult to track down bug. :slight_smile:

Bill Caroselli wrote:

I have just tried SYSPAGE_ENTRY(qtime)->nsec. It appears to be adjusted to
apparent nsec since start of the epoc.

Is this an OS bug or does the documentation need to be updated?

On my PC running QNX 3.2.1B, I found that SYSPAGE_ENTRY(qtime)->nsec corresponds to about how long
since I last booted my PC. (SYSPAGE_ENTRY(qtime)->nsec + SYSPAGE_ENTRY(qtime)->nsec_tod_adjust)
corresponds to about 34.329 years, which pretty much matches the epoch.

Also, when I change the time/date, SYSPAGE_ENTRY(qtime)->nsec keeps increasing at the same rate (ie
not affected by the time change). The nsec + nsec_tod_adjust sum did change to reflect the time
change (34.3 years changed to 34.24 years when I set the month to March instead of April).

Kevin N.

In article <c6u69l$4i5$1@inn.qnx.com>,
Bill Caroselli <qtps@earthlink.net> wrote:

I have just tried SYSPAGE_ENTRY(qtime)->nsec. It appears to be adjusted to
apparent nsec since start of the epoc.

Try the program below. On my machine it shows:

nsec = 104152 (in seconds)
+tod_adjust= 1083370517 (in seconds)
time(NULL) = 1083370517

nsec+nsec_tod_adjust is the current time from the epoch.

=================================================================
#include <stdio.h>
#include <time.h>
#include <sys/syspage.h>

int
main() {
unsigned long long nsec;
unsigned long long tod_adjust;
unsigned long time_res;

// Should loop getting these, but this is just for demo.
nsec = SYSPAGE_ENTRY(qtime)->nsec;
tod_adjust = SYSPAGE_ENTRY(qtime)->nsec_tod_adjust;
time_res = time(NULL);

printf(“nsec =%16llu (in seconds)\n”, nsec / 1000000000);
printf("+tod_adjust=%16llu (in seconds)\n", (nsec+tod_adjust) / 1000000000);
printf(“time(NULL) =%16lu\n”, time_res);
return 0;
}


Brian Stecher (bstecher@qnx.com) QNX Software Systems, Ltd.
phone: +1 (613) 591-0931 (voice) 175 Terence Matthews Cr.
+1 (613) 591-3579 (fax) Kanata, Ontario, Canada K2M 1W8

Brian Stecher <bstecher@qnx.com> wrote:
BS > In article <c6u69l$4i5$1@inn.qnx.com>,
BS > Bill Caroselli <qtps@earthlink.net> wrote:

I have just tried SYSPAGE_ENTRY(qtime)->nsec. It appears to be adjusted to
apparent nsec since start of the epoc.

BS > Try the program below. On my machine it shows:

BS > nsec = 104152 (in seconds)
BS > +tod_adjust= 1083370517 (in seconds)
BS > time(NULL) = 1083370517

BS > nsec+nsec_tod_adjust is the current time from the epoch.

BS > =================================================================
BS > #include <stdio.h>
BS > #include <time.h>
BS > #include <sys/syspage.h>

BS > int
BS > main() {
BS > unsigned long long nsec;
BS > unsigned long long tod_adjust;
BS > unsigned long time_res;

BS > // Should loop getting these, but this is just for demo.
BS > nsec = SYSPAGE_ENTRY(qtime)->nsec;
BS > tod_adjust = SYSPAGE_ENTRY(qtime)->nsec_tod_adjust;
BS > time_res = time(NULL);

BS > printf(“nsec =%16llu (in seconds)\n”, nsec / 1000000000);
BS > printf("+tod_adjust=%16llu (in seconds)\n", (nsec+tod_adjust) / 1000000000);
BS > printf(“time(NULL) =%16lu\n”, time_res);
BS > return 0;
BS > }

OK. I get it now.

The nsec2timespec() function automatically adds in the nsec_tod_adjust.
And I assume that timespec2nsec() will back out nsec_tod_adjust.

Steve: This should be reflected in the docs.

Bill Caroselli wrote:

The nsec2timespec() function automatically adds in the nsec_tod_adjust.
And I assume that timespec2nsec() will back out nsec_tod_adjust.

Steve: This should be reflected in the docs.

Not for me, it doesn’t. Using nsec2timespec() on nsec + nsec_tod_adjust gives me the seconds &
nanoseconds since the epoch. Using nsec2timespec() on just nsec gives the seconds & nanoseconds
since I booted my machine.

Kevin N.

Kevin N <xxxx@yyyy.com> wrote:

KN > Not for me, it doesn’t. Using nsec2timespec() on nsec + nsec_tod_adjust gives me the seconds &
KN > nanoseconds since the epoch. Using nsec2timespec() on just nsec gives the seconds & nanoseconds
KN > since I booted my machine.

Oh Crap! My fault. Don’t even ask.