예제 #1
0
파일: 2-1.c 프로젝트: Mellanox/arc_ltp
int main(int argc, char *argv[])
{
#if !defined(_POSIX_CPUTIME) || _POSIX_CPUTIME == -1
        printf("_POSIX_CPUTIME unsupported\n");
        return PTS_UNSUPPORTED;
#else
	unsigned long time_to_set;
	clockid_t clockid_1, clockid_2;
	struct timespec t1, t2, t3;

	if (sysconf(_SC_CPUTIME) == -1) {
		printf("_POSIX_CPUTIME unsupported\n");
		return PTS_UNSUPPORTED;
	}

	if (clock_getcpuclockid(getpid(), &clockid_1) != 0) {
		printf("clock_getcpuclockid(getpid(),) failed\n");
		return PTS_FAIL;
	}

	if (clock_getcpuclockid(0, &clockid_2) != 0) {
		printf("clock_getcpuclockid(0,) failed\n");
		return PTS_FAIL;
	}

	/* if ids are the same, we are done */
	if (clockid_1 == clockid_2 && clockid_2 == CLOCK_PROCESS_CPUTIME_ID) {
		printf("Test PASSED\n");
		return PTS_PASS;
	}

	/* otherwise get cputimes and check that they differs only a little */
	if (clock_gettime(clockid_1, &t1) != 0) {
		printf("clock_gettime(clockid_1,) failed\n");
		return PTS_FAIL;
	}

	if (clock_gettime(clockid_2, &t2) != 0) {
		printf("clock_gettime(clockid_2,) failed\n");
		return PTS_FAIL;
	}
	
	if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t3) != 0) {
		printf("clock_gettime(CLOCK_PROCESS_CPUTIME_ID,) failed\n");
		return PTS_FAIL;
	}

	if (timespec_nsec_diff(&t1, &t2) > NSEC_IN_SEC/2 ||
	    timespec_nsec_diff(&t2, &t3) > NSEC_IN_SEC/2) {
		printf("reported times differ too much\n");
		return PTS_FAIL;
	}

	printf("Test PASSED\n");
	return PTS_PASS;
#endif
}
예제 #2
0
파일: 2-1.c 프로젝트: SummerSnail2014/haiku
int main(int argc, char *argv[])
{
#if _POSIX_CPUTIME == -1
        printf("_POSIX_CPUTIME unsupported\n");
        return PTS_UNSUPPORTED;
#else
	unsigned long time_to_set;	
	clockid_t clockid_1, clockid_2;
	struct timespec tp1, tp2;

	if (sysconf(_SC_CPUTIME) == -1) {
		printf("_POSIX_CPUTIME unsupported\n");
		return PTS_UNSUPPORTED;
	}

	if (clock_getcpuclockid(getpid(), &clockid_1) != 0) {
		printf("clock_getcpuclockid(getpid(), ) failed\n");
		return PTS_FAIL;
	}
	
	if (clock_getcpuclockid(0, &clockid_2) != 0) {
		printf("clock_getcpuclockid(0, ) failed\n");
		return PTS_FAIL;
	}

	/* Set clockid_1 as a random value from 1 sec to 10 sec */
	srand((unsigned long)time(NULL));
	time_to_set = rand() * 10.0 / RAND_MAX + 1;
	tp1.tv_sec = time_to_set;
	tp1.tv_nsec = 0;	 
	if (clock_settime(clockid_1, &tp1) != 0) {
		printf("clock_getcpuclockid() returned an invalid clockid_t: "
			"%d\n", clockid_1);
		return PTS_FAIL;
	}
	/* Get the time of clockid_2, should almost the same as clockid_1 */
	if (clock_gettime(clockid_2, &tp2) != 0) {
		printf("clock_getcpuclockid() returned an invalid clockid_t: "
			"%d\n", clockid_2);
		return PTS_FAIL;
	}
	if (tp1.tv_sec == tp2.tv_sec)	
	{
		printf("Test PASSED\n");
		return PTS_PASS;
	}
	printf("Test FAILED\n");
	return PTS_FAIL;
	
#endif
}
예제 #3
0
void timer_clock_gettime()
{
/*	POSIX clock
 *  - rt 라이브러리 필요*/

	int ret;
	struct timespec ts;
	// 실제 시간
	ret = clock_gettime(CLOCK_REALTIME, &ts);
	printf("sec: %ld, nanosec: %ld\n", ts.tv_sec, ts.tv_nsec);
	// 시스템 시작 이후 경과 시간
	ret = clock_gettime(CLOCK_MONOTONIC, &ts);
	printf("sec: %ld, nanosec: %ld\n", ts.tv_sec, ts.tv_nsec);
	// 프로세스가 소비한 kernel + user 시간
	ret = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
	printf("sec: %ld, nanosec: %ld\n", ts.tv_sec, ts.tv_nsec);
	// 현재 스레드가 소비한 kernel + user 시간
	ret = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
	printf("sec: %ld, nanosec: %ld\n", ts.tv_sec, ts.tv_nsec);

	// 클록의 정밀도 확인(= 1 nanosecond)
	struct timespec res;
	ret = clock_getres(CLOCK_REALTIME, &res);
	printf("sec: %ld, nanosec: %ld\n", res.tv_sec, res.tv_nsec);

	// 현재 프로세스의 clockid를 구해서 프로세스의 CPU 사용 시간 확인
	// CLOCK_PROCESS_CPUTIME_ID와 뭐가 다른지 모르겠음..
	clockid_t clockid;
	ret = clock_getcpuclockid(getpid(), &clockid);
	printf("clockid: %d\n", clockid);
	ret = clock_gettime(clockid, &ts);
	printf("sec: %ld, nanosec: %ld\n", ts.tv_sec, ts.tv_nsec);

}
예제 #4
0
파일: 1-1.c 프로젝트: CSU-GH/okl4_3.0
int main(int argc, char *argv[])
{
#ifndef _POSIX_CPUTIME
    printf("_POSIX_CPUTIME unsupported\n");
    return PTS_UNSUPPORTED;
#else
    clockid_t clockid;
    struct timespec tp1 = {.tv_sec = 0, .tv_nsec = 0};

    dosomething();

    if (clock_getcpuclockid(getpid(), &clockid) != 0) {
        printf("clock_getcpuclockid() failed\n");
        return PTS_FAIL;
    }

    /* Verify that it returned a valid clockid_t that can be used in other functions */
    if (clock_gettime(clockid, &tp1) != 0) {
        printf("clock_getcpuclockid() returned an invalid clockid_t: %d\n", clockid);
        return PTS_FAIL;
    }

    printf("Test PASSED\n");
    return PTS_PASS;
#endif
}
예제 #5
0
int main()
{
	int	ret;
#ifdef _POSIX_CPUTIME
	struct timespec     ts1, ts2, ts_diff;
	clockid_t	clock_cpu;
	if ((ret = clock_getcpuclockid(0, &clock_cpu)) != 0) { 
		return EXIT_FAILURE;
	}
	clock_gettime(clock_cpu, &ts1);
#endif
	int i;
	double x, step, sum = 0.0;
	step = 1.0/(double) num_steps;
	for (i=0; i<num_steps; i++) {
		x = (i+0.5) * step;
		sum += 4.0/(1.0 + x*x);
	}
	printf("pi = %.8f (sum = %.8f)\n", step*sum, sum);
	sleep(1);
#ifdef _POSIX_CPUTIME
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts2);
	ts_diff = diff_timespec(ts1, ts2);
	printf("elapsed cpu time = %ld.%09ld\n", ts_diff.tv_sec, ts_diff.tv_nsec);
#endif
	return EXIT_SUCCESS;
}
예제 #6
0
void test( void )
{
  pid_t      pid = 0;
  clockid_t  clock_id;
  int        result;

  result = clock_getcpuclockid( pid, &clock_id );
}
예제 #7
0
파일: traces.c 프로젝트: cahirwpz/mneme
void traces_init_hook(void)
{
	while (AO_test_and_set(&initlock) == AO_TS_SET);
		
	if (traces == NULL) {
		DEBUG("inializing");
		
		/* create shared memory block for malloc traces */
		traces_data_t *_traces;
		
		_traces = mmap(NULL, sizeof(traces_log_t) * 4096, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);

		if (_traces == NULL) {
			perror("Cannot create shared memory segment for malloc traces:");
			abort();
		}
		
		DEBUG("traces data placed at $%.8x", (uint32_t)_traces);

		memset(&_traces->logs, 0, (LAST_LOGLINE - 1) * sizeof(traces_log_t));

		/* open log file */
		char *logname = getenv("MALLOC_TRACE_LOG");

		if (logname == NULL)
			logname = "trace-log.bin";
		
		fprintf(stderr, "ptmalloc3 traces logname = %s\n", logname);

		_traces->logfd  = open(logname, O_WRONLY|O_APPEND|O_CREAT, 0600);
		_traces->logcnt = 0;
		_traces->usecnt = 0;
		_traces->lock   = AO_TS_INITIALIZER;

		if (_traces->logfd == -1) {
			perror("Cannot open log file:");
			abort();
		}

		/* get clock id of the process */
		if (clock_getcpuclockid(getpid(), &_traces->clkid) != 0) {
			perror("Cannot obtain clock id:");
			abort();
		}

		traces = _traces;

		DEBUG("initialized");
	} else {
		DEBUG("already initialized");
	}
	
	AO_CLEAR(&initlock);

#if WANT_TO_HAVE_ATEXIT_BUG
	traces_register_at_exit();
#endif
}
예제 #8
0
TEST(time, clock_getcpuclockid) {
  // For current process.
  clockid_t clockid;
  ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));

  timespec ts;
  ASSERT_EQ(0, clock_gettime(clockid, &ts));

  // For parent process.
  ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
  ASSERT_EQ(0, clock_gettime(clockid, &ts));

  // For invalid process.
  // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
  errno = 0;
  ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid));
  ASSERT_EQ(0, errno);
}
int main()
{
	pid_t pid;
	int cpuclkid,input,Pid;
	clockid_t clockid;
	struct timespec ts;

myloop:
	fprintf(stdout,"Enter 1 to get your current program's process id \n");
	fprintf(stdout,"Enter 2 to get others process id \n");
	fprintf(stdout,"Enter 3 get clock ID of the CPU-time clock for specified process \n");
	scanf("%d",&input);
	
	switch(input)
	{
		case 1 : 
			fprintf(stdout,"Your PID %d \n",getpid());
			goto myloop;
			break;
		case 2 :
			if(system("ps -e") == -1)
			{
				perror("system");
				exit(EXIT_FAILURE);
			}
			else
			goto myloop;
			break;
		case 3 :
			fprintf(stdout,"Enter the PID to get the clock ID\n");
			scanf("%d",&Pid);
			pid = Pid;
			cpuclkid = clock_getcpuclockid(pid,&clockid);
			if(cpuclkid)
			{
				perror("clock_getcpuclockid");
				exit(EXIT_FAILURE);
			}
			else
			{
				if (clock_gettime(clockid, &ts) == -1) 
				{
               				perror("clock_gettime");
               				exit(EXIT_FAILURE);
           			}
				else
           				printf("CPU-time clock for PID %d is %ld.%09ld seconds\n",
                   				pid, (long) ts.tv_sec, (long) ts.tv_nsec);
			}
			break;
		default:
			fprintf(stdout,"Wrong Selection \n");
			break;
	}
	return 0;
}
예제 #10
0
/**
* @brief Initialize the system timer
*/
void
init_timer (void)
{
  int err;
  err = clock_getcpuclockid (0, &clockid);
  if (err >= 0)
    return;
  fprintf (stderr, "Unable to find CPU clock, falling back to "
           CLKIDNAME "\n");
  clockid = CLKID;
}
static int
do_test (void)
{
  clockid_t cl;
  int result;

  result = clock_test (CLOCK_REALTIME);

  if (clock_getcpuclockid (0, &cl) == 0)
    /* XXX It's not yet a bug when this fails.  */
    clock_test (cl);

  result = clock_test (CLOCK_PROCESS_CPUTIME_ID);

  if (clock_getcpuclockid (0, &cl) == 0)
    /* XXX It's not yet a bug when this fails.  */
    clock_test (cl);

  return result;
}
예제 #12
0
/**
    Creates the controller quantum timer
*/
int create_timer_controller() {

    timer_t timerid;
    struct sigevent sevt;
    struct itimerspec its;
    sigset_t mask;

    // Establish handler for timer signal
    sigaction_contoller.sa_flags = SA_SIGINFO;
    sigaction_contoller.sa_sigaction = sighandler_timer_controller;
    sigemptyset( &sigaction_contoller.sa_mask );
    if( sigaction( SIG_TIMER_CONTROLLER, &sigaction_contoller, NULL ) == -1 )
        return ERR_TIMER_FAILURE_SIGACTION;

    // Block timer signal temporarily
    sigemptyset( &mask );
    sigaddset( &mask, SIG_TIMER_CONTROLLER );
    if( sigprocmask( SIG_SETMASK, &mask, NULL ) == -1 )
        return ERR_TIMER_FAILURE_SIGPROCMASK;

    if( clock_getcpuclockid( controller_pid, &controller_clock ) != 0 )
        return ERR_TIMER_FAILURE_GETCLOCKID;

    // Create the timer
    sevt.sigev_notify = SIGEV_SIGNAL;
    sevt.sigev_signo = SIG_TIMER_CONTROLLER;
    sevt.sigev_value.sival_ptr = &timerid;
    //if( timer_create( CLOCK_REALTIME, &sevt, &timerid ) == -1 )
    if( timer_create( controller_clock, &sevt, &timerid ) == -1 )
        return ERR_TIMER_FAILURE_CREATE;

    // Start the timer
    its.it_value.tv_sec = 1;                                   // initial delay
    its.it_value.tv_nsec = 0;
    //its.it_interval.tv_sec = DEFAULT_SEC_QUANTUM_CONTROLLER;
    //its.it_interval.tv_nsec = 0;

    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = DEFAULT_NSEC_QUANTUM_CONTROLLER;  // regular quantum

    if( timer_settime( timerid, 0, &its, NULL ) == -1 )
        return ERR_TIMER_FAILURE_SETTIME;

    // Unblock the timer
    if( sigprocmask( SIG_UNBLOCK, &mask, NULL ) == -1 )
        return ERR_TIMER_FAILURE_SIGPROCMASK;

    return ERR_TIMER_NOERROR;
}
예제 #13
0
파일: uprof.c 프로젝트: ebassi/UProf
void
uprof_init_real (void)
{
  static gboolean initialized = FALSE;

  if (initialized)
    return;

  g_type_init ();

#ifndef USE_RDTSC
  int ret;
  struct timespec ts;

  ret = clock_getcpuclockid(0, &clockid);
  if (ret == ENOENT)
    {
      g_warning ("Using the CPU clock will be unreliable on this system if "
                 "you don't assure processor affinity");
    }
  else if (ret != 0)
    {
      const char *str = strerror (errno);
      g_warning ("Failed to get CPU clock ID: %s", str);
    }

  if (clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts) == -1)
    {
      const char *str = strerror (errno);
      g_warning ("Failed to query CLOCK_PROCESS_CPUTIME_ID clock: %s", str);
    }
#endif

  mainloop_context = uprof_context_new ("Mainloop context");

  dbus_g_object_register_marshaller (_uprof_marshal_VOID__STRING_STRING,
                                     G_TYPE_NONE,
                                     G_TYPE_STRING,
                                     G_TYPE_STRING,
                                     G_TYPE_INVALID);

  _uprof_report_register_dbus_type_info ();
  _uprof_service_register_dbus_type_info ();

  service = _uprof_service_new ();

  initialized = TRUE;
}
예제 #14
0
static int
do_test (void)
{
  clockid_t cl;
  int result;

  result = clock_test (CLOCK_REALTIME);

  if (clock_getcpuclockid (0, &cl) == 0)
    /* XXX It's not yet a bug when this fails.  */
    clock_test (cl);
  else
	  printf("CPU clock unavailble, skipping test\n");

  return result;
}
예제 #15
0
int create_controller_timer() {
    timer_t timerid;
    struct sigevent sevt;
    struct itimerspec its;

    // Establish handler for timer signal
    sigaction_contoller.sa_flags = SA_SIGINFO;
    sigaction_contoller.sa_sigaction = sighandler_timer_controller;
    sigemptyset( &sigaction_contoller.sa_mask );
    if( sigaction( SIG_TIMER_CONTROLLER, &sigaction_contoller, NULL ) == -1 )
       return ERR_TIMER_FAILURE_SIGACTION;

    // intialize the signal mask
    sigemptyset( &controller_timer_mask );
    sigaddset( &controller_timer_mask, SIG_TIMER_CONTROLLER );

    // Block timer signal temporarily
    if( block_controller_timer() != ERR_TIMER_NOERROR )
        return ERR_TIMER_FAILURE_SIGPROCMASK;

    if( clock_getcpuclockid( controller_pid, &controller_clock ) != 0 )
        return ERR_TIMER_FAILURE_GETCLOCKID;

    // Create the timer
    sevt.sigev_notify = SIGEV_SIGNAL;
    sevt.sigev_signo = SIG_TIMER_CONTROLLER;
    sevt.sigev_value.sival_ptr = &timerid;
    if( timer_create( controller_clock, &sevt, &timerid ) == -1 )
       return ERR_TIMER_FAILURE_CREATE;

    // intial delay for the timer
    its.it_value.tv_sec = DEFAULT_INITDELAY_CONTROLLER_SEC;
    its.it_value.tv_nsec = DEFAULT_INITDELAY_CONTROLLER_NSEC;
    // quantum interval for the timer
    // NOTE: technically in this implementation this is a tick not the quantum
    its.it_interval.tv_sec = DEFAULT_QUANTUM_CONTROLLER_SEC;
    its.it_interval.tv_nsec = DEFAULT_QUANTUM_CONTROLLER_NSEC;

    // Set up the timer
    if( timer_settime( timerid, 0, &its, NULL ) == -1 )
        return ERR_TIMER_FAILURE_SETTIME;

    // NOTE: The timer is blocked at this point.  It must be unblocked in
    // the parent process for the timer to function

    return ERR_TIMER_NOERROR;
}
예제 #16
0
파일: hw09.c 프로젝트: alarya/Bash-Scripts
void childCpuTime(pid_t child)
{
	clockid_t clockid;
	struct timespec ts;

	if(clock_getcpuclockid(child, &clockid) != 0) 
	{
        perror("clock_getcpuclockid");
        exit(EXIT_FAILURE);
    }
	if (clock_gettime(clockid, &ts) == -1) 
	{
        perror("clock_gettime");
        exit(EXIT_FAILURE);
    }

    printf("Child took: %ld.%09ld seconds\n", (long) ts.tv_sec, (long) ts.tv_nsec);
}
예제 #17
0
파일: 5-1.c 프로젝트: 1587/ltp
int main(void)
{
#if !defined(_POSIX_CPUTIME) || _POSIX_CPUTIME == -1
	printf("_POSIX_CPUTIME unsupported\n");
	return PTS_UNSUPPORTED;
#else
	clockid_t clockid_1;
	int error;

	/*
	 * Do a best effort at trying to get root demoted to "nobody" for the
	 * duration of the test.
	 */
	if (getuid() == 0) {
		struct passwd *pwd;

		pwd = getpwnam("nobody");
		if (pwd != NULL) {
			setgid(pwd->pw_gid);
			setuid(pwd->pw_uid);
		}
	}

	/* Hmmm -- above steps failed :(... */
	if (getuid() == 0) {
		printf("Test must be run as non-root user\n");
		return PTS_UNRESOLVED;
	}

	/* Try and get the cpu clock ID for init(1) :)... */
	error = clock_getcpuclockid(1, &clockid_1);
	if (error == 0) {
		printf("clock_getcpuclockid(1, ..) passed\n");
		return PTS_UNTESTED;
	} else if (error != EPERM) {
		printf("clock_getcpuclockid(1, ..) failed with an improper "
		       "error (%d != %d)\n", EPERM, error);
		return PTS_UNRESOLVED;
	}
	printf("Test PASSED\n");
	return PTS_PASS;
#endif
}
예제 #18
0
파일: 1-3.c 프로젝트: shubmit/shub-ltp
int main()
{

	#if _POSIX_CPUTIME == -1
		printf("_POSIX_CPUTIME unsupported\n");
		return PTS_UNSUPPORTED;
	#endif

	pthread_condattr_t condattr;
	clockid_t clockid;
	int rc;

	if (sysconf(_SC_CPUTIME) == -1) {
		printf("_POSIX_CPUTIME unsupported\n");
		return PTS_UNSUPPORTED;
	}

	/* Initialize a cond attributes object */
	if ((rc=pthread_condattr_init(&condattr)) != 0)
	{
		fprintf(stderr,"Error at pthread_condattr_init(), rc=%d\n",rc);
		printf("Test FAILED\n");
		return PTS_FAIL;
	}

	/* Get the cpu clock id */

	if (clock_getcpuclockid(getpid(), &clockid) != 0)
	{
		printf("clock_getcpuclockid() failed\n");
		return PTS_FAIL;
	}

	rc = pthread_condattr_setclock(&condattr, clockid);
	if (rc != EINVAL)
	{
		printf("Test FAILED: Expected EINVAL when passing a cpu clock id, instead it returned: %d \n", rc);
		return PTS_FAIL;
	}

	printf("Test PASSED\n");
	return PTS_PASS;
}
예제 #19
0
int main() {

  clockid_t clockid = 0;
  if(clock_getcpuclockid(0,&clockid) == 0) {
    switch(clockid) {
      case CLOCK_MONOTONIC:
        printf("CLOCK_MONOTONIC\n");
        break;
      case CLOCK_REALTIME:
        printf("CLOCK_REALTIME\n");
        break;
      default:
        printf("Got some other clock %d, setting to CLOCK_REALTIME\n",clockid);
        clockid = CLOCK_REALTIME;
    }
  }
	
  struct timespec start, stop;
  struct timeval t_start, t_stop;
  memset(&start,0,sizeof(struct timespec));
  memset(&stop,0,sizeof(struct timespec));
  memset(&t_start,0,sizeof(struct timeval));
  memset(&t_stop,0,sizeof(struct timeval));

  clock_gettime(clockid, &start);
  gettimeofday(&t_start,NULL);

  usleep(100*1000);

  clock_gettime(clockid, &stop);
  gettimeofday(&t_stop,NULL);

  printf("clock_gettime() difference %lu ms (%lu us)\n",timespec_difference_ms(&start,&stop),timespec_difference_us(&start,&stop));
  printf("gettimeofday() difference %lu ms (%lu us)\n",timeval_difference_ms(&t_start,&t_stop),timeval_difference_us(&t_start,&t_stop));
	
  return 0;
}
예제 #20
0
/**
 * Returns the amount of CPU time used by the current process,
 * in seconds, or -1.0 if an error occurred.
 */
double getCPUTime( )
{
#if defined(_WIN32)
    /* Windows -------------------------------------------------- */
    FILETIME createTime;
    FILETIME exitTime;
    FILETIME kernelTime;
    FILETIME userTime;
    if ( GetProcessTimes( GetCurrentProcess( ),
                          &createTime, &exitTime, &kernelTime, &userTime ) != -1 ) {
        SYSTEMTIME userSystemTime;
        if ( FileTimeToSystemTime( &userTime, &userSystemTime ) != -1 )
            return (double)userSystemTime.wHour * 3600.0 +
                   (double)userSystemTime.wMinute * 60.0 +
                   (double)userSystemTime.wSecond +
                   (double)userSystemTime.wMilliseconds / 1000.0;
    }
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
    /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, and Solaris --------- */
#if _POSIX_TIMERS > 0
    /* Prefer high-res POSIX timers, when available. */
    {
        clockid_t id;
        struct timespec ts;
#if _POSIX_CPUTIME > 0
        /* Clock ids vary by OS.  Query the id, if possible. */
        if ( clock_getcpuclockid( 0, &id ) == -1 )
#endif
#if defined(CLOCK_PROCESS_CPUTIME_ID)
            /* Use known clock id for AIX, Linux, or Solaris. */
            id = CLOCK_PROCESS_CPUTIME_ID;
#elif defined(CLOCK_VIRTUAL)
            /* Use known clock id for BSD or HP-UX. */
            id = CLOCK_VIRTUAL;
#else
            id = (clockid_t) - 1;
#endif
        if ( id != (clockid_t) - 1 && clock_gettime( id, &ts ) != -1 )
            return (double)ts.tv_sec +
                   (double)ts.tv_nsec / 1000000000.0;
    }
#endif
#if defined(RUSAGE_SELF)
    {
        struct rusage rusage;
        if ( getrusage( RUSAGE_SELF, &rusage ) != -1 )
            return (double)rusage.ru_utime.tv_sec +
                   (double)rusage.ru_utime.tv_usec / 1000000.0;
    }
#endif
#if defined(_SC_CLK_TCK)
    {
        const double ticks = (double)sysconf( _SC_CLK_TCK );
        struct tms tms;
        if ( times( &tms ) != (clock_t) - 1 )
            return (double)tms.tms_utime / ticks;
    }
#endif
#if defined(CLOCKS_PER_SEC)
    {
        clock_t cl = clock( );
        if ( cl != (clock_t) - 1 )
            return (double)cl / (double)CLOCKS_PER_SEC;
    }
#endif
#endif
    return -1.0;        /* Failed. */
}
예제 #21
0
static int
do_test (void)
{
  int result = 0;
  clockid_t cl;
  int e;
  pid_t dead_child, child;

  /* Fork a child and let it die, to give us a PID known not be valid
     (assuming PIDs don't wrap around during the test).  */
  {
    dead_child = fork ();
    if (dead_child == 0)
      _exit (0);
    if (dead_child < 0)
      {
	perror ("fork");
	return 1;
      }
    int x;
    if (wait (&x) != dead_child)
      {
	perror ("wait");
	return 2;
      }
  }

  /* POSIX says we should get ESRCH for this.  */
  e = clock_getcpuclockid (dead_child, &cl);
  if (e != ENOSYS && e != ESRCH && e != EPERM)
    {
      printf ("clock_getcpuclockid on dead PID %d => %s\n",
	      dead_child, strerror (e));
      result = 1;
    }

  /* Now give us a live child eating up CPU time.  */
  child = fork ();
  if (child == 0)
    {
      chew_cpu ();
      _exit (1);
    }
  if (child < 0)
    {
      perror ("fork");
      return 1;
    }

  e = clock_getcpuclockid (child, &cl);
  if (e == EPERM)
    {
      puts ("clock_getcpuclockid does not support other processes");
      goto done;
    }
  if (e != 0)
    {
      printf ("clock_getcpuclockid on live PID %d => %s\n",
	      child, strerror (e));
      result = 1;
      goto done;
    }

  const clockid_t child_clock = cl;
  struct timespec res;
  if (clock_getres (child_clock, &res) < 0)
    {
      printf ("clock_getres on live PID %d clock %lx => %s\n",
	      child, (unsigned long int) child_clock, strerror (errno));
      result = 1;
      goto done;
    }
  printf ("live PID %d clock %lx resolution %ju.%.9ju\n",
	  child, (unsigned long int) child_clock,
	  (uintmax_t) res.tv_sec, (uintmax_t) res.tv_nsec);

  struct timespec before, after;
  if (clock_gettime (child_clock, &before) < 0)
    {
      printf ("clock_gettime on live PID %d clock %lx => %s\n",
	      child, (unsigned long int) child_clock, strerror (errno));
      result = 1;
      goto done;
    }
  /* Should be close to 0.0.  */
  printf ("live PID %d before sleep => %ju.%.9ju\n",
	  child, (uintmax_t) before.tv_sec, (uintmax_t) before.tv_nsec);

  struct timespec sleeptime = { .tv_nsec = 500000000 };
  if (nanosleep (&sleeptime, NULL) != 0)
    {
      perror ("nanosleep");
      result = 1;
      goto done;
    }

  if (clock_gettime (child_clock, &after) < 0)
    {
      printf ("clock_gettime on live PID %d clock %lx => %s\n",
	      child, (unsigned long int) child_clock, strerror (errno));
      result = 1;
      goto done;
    }
  /* Should be close to 0.5.  */
  printf ("live PID %d after sleep => %ju.%.9ju\n",
	  child, (uintmax_t) after.tv_sec, (uintmax_t) after.tv_nsec);

  struct timespec diff = { .tv_sec = after.tv_sec - before.tv_sec,
			   .tv_nsec = after.tv_nsec - before.tv_nsec };
  if (diff.tv_nsec < 0)
    {
      --diff.tv_sec;
      diff.tv_nsec += 1000000000;
    }
  if (diff.tv_sec != 0
      || diff.tv_nsec > 600000000
      || diff.tv_nsec < 100000000)
    {
      printf ("before - after %ju.%.9ju outside reasonable range\n",
	      (uintmax_t) diff.tv_sec, (uintmax_t) diff.tv_nsec);
      result = 1;
    }

  sleeptime.tv_nsec = 100000000;
  e = clock_nanosleep (child_clock, 0, &sleeptime, NULL);
  if (e == EINVAL || e == ENOTSUP || e == ENOSYS)
    {
      printf ("clock_nanosleep not supported for other process clock: %s\n",
	      strerror (e));
    }
  else if (e != 0)
    {
      printf ("clock_nanosleep on other process clock: %s\n", strerror (e));
      result = 1;
    }
  else
    {
      struct timespec afterns;
      if (clock_gettime (child_clock, &afterns) < 0)
	{
	  printf ("clock_gettime on live PID %d clock %lx => %s\n",
		  child, (unsigned long int) child_clock, strerror (errno));
	  result = 1;
	}
      else
	{
	  struct timespec d = { .tv_sec = afterns.tv_sec - after.tv_sec,
				.tv_nsec = afterns.tv_nsec - after.tv_nsec };
	  if (d.tv_nsec < 0)
	    {
	      --d.tv_sec;
	      d.tv_nsec += 1000000000;
	    }
	  if (d.tv_sec > 0
	      || d.tv_nsec < sleeptime.tv_nsec
	      || d.tv_nsec > sleeptime.tv_nsec * 2)
	    {
	      printf ("nanosleep time %ju.%.9ju outside reasonable range\n",
		      (uintmax_t) d.tv_sec, (uintmax_t) d.tv_nsec);
	      result = 1;
	    }
	}
    }

  if (kill (child, SIGKILL) != 0)
    {
      perror ("kill");
      result = 2;
      goto done;
    }

  /* Wait long enough to let the child finish dying.  */

  sleeptime.tv_nsec = 200000000;
  if (nanosleep (&sleeptime, NULL) != 0)
    {
      perror ("nanosleep");
      result = 1;
      goto done;
    }

  struct timespec dead;
  if (clock_gettime (child_clock, &dead) < 0)
    {
      printf ("clock_gettime on dead PID %d clock %lx => %s\n",
	      child, (unsigned long int) child_clock, strerror (errno));
      result = 1;
      goto done;
    }
  /* Should be close to 0.6.  */
  printf ("dead PID %d => %ju.%.9ju\n",
	  child, (uintmax_t) dead.tv_sec, (uintmax_t) dead.tv_nsec);

  diff.tv_sec = dead.tv_sec - after.tv_sec;
  diff.tv_nsec = dead.tv_nsec - after.tv_nsec;
  if (diff.tv_nsec < 0)
    {
      --diff.tv_sec;
      diff.tv_nsec += 1000000000;
    }
  if (diff.tv_sec != 0 || diff.tv_nsec > 200000000)
    {
      printf ("dead - after %ju.%.9ju outside reasonable range\n",
	      (uintmax_t) diff.tv_sec, (uintmax_t) diff.tv_nsec);
      result = 1;
    }

  /* Now reap the child and verify that its clock is no longer valid.  */
  {
    int x;
    if (waitpid (child, &x, 0) != child)
      {
	perror ("waitpid");
	result = 1;
      }
  }

  if (clock_gettime (child_clock, &dead) == 0)
    {
      printf ("clock_gettime on reaped PID %d clock %lx => %ju%.9ju\n",
	      child, (unsigned long int) child_clock,
	      (uintmax_t) dead.tv_sec, (uintmax_t) dead.tv_nsec);
      result = 1;
    }
  else
    {
      if (errno != EINVAL)
	result = 1;
      printf ("clock_gettime on reaped PID %d clock %lx => %s\n",
	      child, (unsigned long int) child_clock, strerror (errno));
    }

  if (clock_getres (child_clock, &dead) == 0)
    {
      printf ("clock_getres on reaped PID %d clock %lx => %ju%.9ju\n",
	      child, (unsigned long int) child_clock,
	      (uintmax_t) dead.tv_sec, (uintmax_t) dead.tv_nsec);
      result = 1;
    }
  else
    {
      if (errno != EINVAL)
	result = 1;
      printf ("clock_getres on reaped PID %d clock %lx => %s\n",
	      child, (unsigned long int) child_clock, strerror (errno));
    }

  return result;

 done:
  {
    if (kill (child, SIGKILL) != 0 && errno != ESRCH)
      {
	perror ("kill");
	return 2;
      }
    int x;
    if (waitpid (child, &x, 0) != child && errno != ECHILD)
      {
	perror ("waitpid");
	return 2;
      }
  }

  return result;
}
예제 #22
0
static unsigned long long int
tsdiff (const struct timespec *before, const struct timespec *after)
{
  struct timespec diff = { .tv_sec = after->tv_sec - before->tv_sec,
			   .tv_nsec = after->tv_nsec - before->tv_nsec };
  while (diff.tv_nsec < 0)
    {
      --diff.tv_sec;
      diff.tv_nsec += 1000000000;
    }
  return diff.tv_sec * 1000000000ULL + diff.tv_nsec;
}

static unsigned long long int
test_nanosleep (clockid_t clock, const char *which,
		const struct timespec *before, int *bad)
{
  const struct timespec sleeptime = { .tv_nsec = 100000000 };
  int e = clock_nanosleep (clock, 0, &sleeptime, NULL);
  if (e == EINVAL || e == ENOTSUP || e == ENOSYS)
    {
      printf ("clock_nanosleep not supported for %s CPU clock: %s\n",
	      which, strerror (e));
      return 0;
    }
  if (e != 0)
    {
      printf ("clock_nanosleep on %s CPU clock: %s\n", which, strerror (e));
      *bad = 1;
      return 0;
    }

  struct timespec after;
  if (clock_gettime (clock, &after) < 0)
    {
      printf ("clock_gettime on %s CPU clock %lx => %s\n",
	      which, (unsigned long int) clock, strerror (errno));
      *bad = 1;
      return 0;
    }

  unsigned long long int diff = tsdiff (before, &after);
  if (diff < sleeptime.tv_nsec || diff > sleeptime.tv_nsec * 2)
    {
      printf ("clock_nanosleep on %s slept %llu (outside reasonable range)\n",
	      which, diff);
      *bad = 1;
      return diff;
    }

  struct timespec sleeptimeabs = sleeptime;
  sleeptimeabs.tv_sec += after.tv_sec;
  sleeptimeabs.tv_nsec += after.tv_nsec;
  while (sleeptimeabs.tv_nsec >= 1000000000)
    {
      ++sleeptimeabs.tv_sec;
      sleeptimeabs.tv_nsec -= 1000000000;
    }
  e = clock_nanosleep (clock, TIMER_ABSTIME, &sleeptimeabs, NULL);
  if (e != 0)
    {
      printf ("absolute clock_nanosleep on %s CPU clock: %s\n",
	      which, strerror (e));
      *bad = 1;
      return diff;
    }

  struct timespec afterabs;
  if (clock_gettime (clock, &afterabs) < 0)
    {
      printf ("clock_gettime on %s CPU clock %lx => %s\n",
	      which, (unsigned long int) clock, strerror (errno));
      *bad = 1;
      return diff;
    }

  unsigned long long int sleepdiff = tsdiff (&sleeptimeabs, &afterabs);
  if (sleepdiff > sleeptime.tv_nsec)
    {
      printf ("\
absolute clock_nanosleep on %s %llu past target (outside reasonable range)\n",
	      which, sleepdiff);
      *bad = 1;
    }

  unsigned long long int diffabs = tsdiff (&after, &afterabs);
  if (diffabs < sleeptime.tv_nsec || diffabs > sleeptime.tv_nsec * 2)
    {
      printf ("\
absolute clock_nanosleep on %s slept %llu (outside reasonable range)\n",
	      which, diffabs);
      *bad = 1;
    }

  return diff + diffabs;
}



static int
do_test (void)
{
  int result = 0;
  clockid_t process_clock, th_clock, my_thread_clock;
  int e;
  pthread_t th;

  e = clock_getcpuclockid (0, &process_clock);
  if (e != 0)
    {
      printf ("clock_getcpuclockid on self => %s\n", strerror (e));
      return 1;
    }

  e = pthread_getcpuclockid (pthread_self (), &my_thread_clock);
  if (e != 0)
    {
      printf ("pthread_getcpuclockid on self => %s\n", strerror (e));
      return 1;
    }

  /* This is a kludge.  This test fails if the semantics of thread and
     process clocks are wrong.  The old code using hp-timing without kernel
     support has bogus semantics if there are context switches.  We don't
     fail to report failure when the proper functionality is not available
     in the kernel.  It so happens that Linux kernels without correct CPU
     clock support also lack CPU timer support, so we use use that to guess
     that we are using the bogus code and not test it.  */
  timer_t t;
  if (timer_create (my_thread_clock, NULL, &t) != 0)
    {
      printf ("timer_create: %m\n");
      puts ("No support for CPU clocks with good semantics, skipping test");
      return 0;
    }
  timer_delete (t);


  pthread_barrier_init (&barrier, NULL, 2);

  e = pthread_create (&th, NULL, chew_cpu, NULL);
  if (e != 0)
    {
      printf ("pthread_create: %s\n", strerror (e));
      return 1;
    }

  e = pthread_getcpuclockid (th, &th_clock);
  if (e == ENOENT || e == ENOSYS || e == ENOTSUP)
    {
      puts ("pthread_getcpuclockid does not support other threads");
      return 1;
    }

  pthread_barrier_wait (&barrier);

  struct timespec res;
  if (clock_getres (th_clock, &res) < 0)
    {
      printf ("clock_getres on live thread clock %lx => %s\n",
	      (unsigned long int) th_clock, strerror (errno));
      result = 1;
      return 1;
    }
  printf ("live thread clock %lx resolution %lu.%.9lu\n",
	  (unsigned long int) th_clock, res.tv_sec, res.tv_nsec);

  struct timespec process_before, process_after;
  if (clock_gettime (process_clock, &process_before) < 0)
    {
      printf ("clock_gettime on process clock %lx => %s\n",
	      (unsigned long int) process_clock, strerror (errno));
      return 1;
    }

  struct timespec before, after;
  if (clock_gettime (th_clock, &before) < 0)
    {
      printf ("clock_gettime on live thread clock %lx => %s\n",
	      (unsigned long int) th_clock, strerror (errno));
      return 1;
    }
  printf ("live thread before sleep => %lu.%.9lu\n",
	  before.tv_sec, before.tv_nsec);

  struct timespec me_before, me_after;
  if (clock_gettime (my_thread_clock, &me_before) < 0)
    {
      printf ("clock_gettime on self thread clock %lx => %s\n",
	      (unsigned long int) my_thread_clock, strerror (errno));
      return 1;
    }
  printf ("self thread before sleep => %lu.%.9lu\n",
	  me_before.tv_sec, me_before.tv_nsec);

  struct timespec sleeptime = { .tv_nsec = 500000000 };
  if (nanosleep (&sleeptime, NULL) != 0)
    {
      perror ("nanosleep");
      return 1;
    }

  if (clock_gettime (th_clock, &after) < 0)
    {
      printf ("clock_gettime on live thread clock %lx => %s\n",
	      (unsigned long int) th_clock, strerror (errno));
      return 1;
    }
  printf ("live thread after sleep => %lu.%.9lu\n",
	  after.tv_sec, after.tv_nsec);

  if (clock_gettime (process_clock, &process_after) < 0)
    {
      printf ("clock_gettime on process clock %lx => %s\n",
	      (unsigned long int) process_clock, strerror (errno));
      return 1;
    }

  if (clock_gettime (my_thread_clock, &me_after) < 0)
    {
      printf ("clock_gettime on self thread clock %lx => %s\n",
	      (unsigned long int) my_thread_clock, strerror (errno));
      return 1;
    }
  printf ("self thread after sleep => %lu.%.9lu\n",
	  me_after.tv_sec, me_after.tv_nsec);

  unsigned long long int th_diff = tsdiff (&before, &after);
  unsigned long long int pdiff = tsdiff (&process_before, &process_after);
  unsigned long long int my_diff = tsdiff (&me_before, &me_after);

  if (th_diff < 100000000 || th_diff > 600000000)
    {
      printf ("live thread before - after %llu outside reasonable range\n",
	      th_diff);
      result = 1;
    }

  if (my_diff > 100000000)
    {
      printf ("self thread before - after %llu outside reasonable range\n",
	      my_diff);
      result = 1;
    }

  if (pdiff < th_diff)
    {
      printf ("process before - after %llu outside reasonable range (%llu)\n",
	      pdiff, th_diff);
      result = 1;
    }

  process_after.tv_nsec += test_nanosleep (th_clock, "live thread",
					   &after, &result);
  process_after.tv_nsec += test_nanosleep (process_clock, "process",
					   &process_after, &result);
  test_nanosleep (CLOCK_PROCESS_CPUTIME_ID,
		  "PROCESS_CPUTIME_ID", &process_after, &result);

  pthread_cancel (th);

  e = clock_nanosleep (CLOCK_THREAD_CPUTIME_ID, 0, &sleeptime, NULL);
  if (e != EINVAL)
    {
      printf ("clock_nanosleep CLOCK_THREAD_CPUTIME_ID: %s\n",
	      strerror (e));
      result = 1;
    }

  return result;
}
예제 #23
0
파일: 22-1.c 프로젝트: 1587/ltp
int main(void)
{
	int ret, status;
	pid_t child, ctl;

	long ctp, ctt;
	clockid_t clp, clt;

	struct timespec tp;

	output_init();

	ctp = sysconf(_SC_CPUTIME);
	ctt = sysconf(_SC_THREAD_CPUTIME);

	if ((ctp == -1) && (ctt == -1)) {
		UNTESTED
		    ("The testcase needs CPUTIME or THREAD_CPUTIME support");
	}
#if VERBOSE > 0
	output("System abilities:\n");

	output("  _POSIX_CPUTIME        : %ld\n", ctp);

	output("  _POSIX_THREAD_CPUTIME : %ld\n", ctt);

#endif
	if (ctp > 0) {
		ret = clock_getcpuclockid(0, &clp);

		if (ret != 0) {
			UNRESOLVED(ret,
				   "Unable to get cpu-time clock id of the process");
		}

		do {
			ret = clock_gettime(clp, &tp);

			if (ret != 0) {
				UNRESOLVED(errno,
					   "Failed to read CPU time clock");
			}
		}
		while (tp.tv_sec < 1);
	}

	if (ctt > 0) {
		ret = pthread_getcpuclockid(pthread_self(), &clt);

		if (ret != 0) {
			UNRESOLVED(ret,
				   "Unable to get cpu-time clock id of the thread");
		}

		do {
			ret = clock_gettime(clt, &tp);

			if (ret != 0) {
				UNRESOLVED(errno,
					   "Failed to read thread CPU time clock");
			}
		}
		while (tp.tv_sec < 1);
	}

	/* Create the child */
	child = fork();

	if (child == -1) {
		UNRESOLVED(errno, "Failed to fork");
	}

	/* child */
	if (child == 0) {
		if (ctp > 0) {
			ret = clock_getcpuclockid(0, &clp);

			if (ret != 0) {
				UNRESOLVED(ret,
					   "Unable to get cpu-time clock id of the process");
			}

			ret = clock_gettime(clp, &tp);

			if (ret != 0) {
				UNRESOLVED(errno,
					   "Failed to read CPU time clock");
			}

			if (tp.tv_sec > 0) {
				FAILED
				    ("The process CPU-time clock was not reset in child\n");
			}
		}

		if (ctt > 0) {
			ret = pthread_getcpuclockid(pthread_self(), &clt);

			if (ret != 0) {
				UNRESOLVED(ret,
					   "Unable to get cpu-time clock id of the thread");
			}

			ret = clock_gettime(clt, &tp);

			if (ret != 0) {
				UNRESOLVED(errno,
					   "Failed to read thread CPU time clock");
			}

			if (tp.tv_sec > 0) {
				FAILED
				    ("The thread CPU-time clock was not reset in child\n");
			}
		}

		exit(PTS_PASS);
	}

	/* Parent joins the child */
	ctl = waitpid(child, &status, 0);

	if (ctl != child) {
		UNRESOLVED(errno, "Waitpid returned the wrong PID");
	}

	if (!WIFEXITED(status) || (WEXITSTATUS(status) != PTS_PASS)) {
		FAILED("Child exited abnormally");
	}

#if VERBOSE > 0
	output("Test passed\n");
#endif
	PASSED;
}
예제 #24
0
파일: init.c 프로젝트: AlexShiLucky/rtems
void *POSIX_Init(
  void *argument
)
{
  int             sc;

  TEST_BEGIN();

  puts( "lio_listio -- ENOSYS" );
  sc = lio_listio( 0, NULL, 0, NULL );
  check_enosys( sc );

  puts( "aio_suspend -- ENOSYS" );
  sc = aio_suspend( NULL, 0, NULL );
  check_enosys( sc );

  puts( "clock_getcpuclockid -- ENOSYS" );
  sc = clock_getcpuclockid( 0, NULL );
  check_enosys( sc );

  puts( "clock_getenable_attr -- ENOSYS" );
  sc = clock_getenable_attr( 0, NULL );
  check_enosys( sc );

  puts( "clock_setenable_attr -- ENOSYS" );
  sc = clock_setenable_attr( 0, 0 );
  check_enosys( sc );

  puts( "execl -- ENOSYS" );
  sc = execl( NULL, NULL, (char*)0 );
  check_enosys( sc );

  puts( "execle -- ENOSYS" );
  sc = execle( NULL, NULL, (char*)0, NULL );
  check_enosys( sc );

  puts( "execlp -- ENOSYS" );
  sc = execlp( NULL, NULL, (char*)0 );
  check_enosys( sc );

  puts( "execv -- ENOSYS" );
  sc = execv( NULL, NULL );
  check_enosys( sc );

  puts( "execve -- ENOSYS" );
  sc = execve( NULL, NULL, NULL );
  check_enosys( sc );

  puts( "execvp -- ENOSYS" );
  sc = execvp( NULL, NULL );
  check_enosys( sc );

  puts( "fork -- ENOSYS" );
  sc = fork();
  check_enosys( sc );

  puts( "pthread_atfork -- ENOSYS" );
  sc = pthread_atfork( NULL, NULL, NULL );
  check_enosys( sc );

  puts( "pthread_getcpuclockid -- ENOSYS" );
  sc = pthread_getcpuclockid( 0, NULL );
  check_enosys( sc );

  puts( "sched_setparam -- ENOSYS" );
  sc = sched_setparam( 0, NULL );
  check_enosys( sc );

  puts( "sched_getparam -- ENOSYS" );
  sc = sched_getparam( 0, NULL );
  check_enosys( sc );

  puts( "sched_setscheduler -- ENOSYS" );
  sc = sched_setscheduler( 0, 0, NULL );
  check_enosys( sc );

  puts( "sched_getscheduler -- ENOSYS" );
  sc = sched_getscheduler( 0 );
  check_enosys( sc );

  puts( "wait -- ENOSYS" );
  sc = wait( NULL );
  check_enosys( sc );

  puts( "waitpid -- ENOSYS" );
  sc = waitpid( 0, NULL, 0 );
  check_enosys( sc );

  puts( "mprotect -- stub implementation - OK" );
  sc = mprotect( NULL, 0, 0 );
  posix_service_failed( sc, "mprotect" );

  puts( "vfork -- stub implementation - OK" );
  sc = vfork();
  if ( sc != -1 ) {
    puts( "vfork did not return -1" );
    rtems_test_exit( 0 );
  }

  TEST_END();
  rtems_test_exit( 0 );

  return NULL; /* just so the compiler thinks we returned something */
}
예제 #25
0
파일: cpuclock3.c 프로젝트: pzz2011/test
static unsigned long long int
tsdiff (const struct timespec *before, const struct timespec *after)
{
    struct timespec diff = { .tv_sec = after->tv_sec - before->tv_sec,
               .tv_nsec = after->tv_nsec - before->tv_nsec
    };
    while (diff.tv_nsec < 0)
    {
        --diff.tv_sec;
        diff.tv_nsec += 1000000000;
    }
    return diff.tv_sec * 1000000000ULL + diff.tv_nsec;
}

int
main (void)
{
    int result = 0;
    clockid_t process_clock;
    int e;
    pthread_t th;

    e = clock_getcpuclockid (0, &process_clock);
    if (e != 0)
    {
        printf ("clock_getcpuclockid on self => %s\n", strerror (e));
        return 1;
    }

    pthread_barrier_init (&barrier, NULL, 2);

    e = pthread_create (&th, NULL, chew_cpu, NULL);
    if (e != 0)
    {
        printf ("pthread_create: %s\n", strerror (e));
        return 1;
    }
    pthread_barrier_wait (&barrier);

    struct timespec process_before;
    if (clock_gettime (process_clock, &process_before) < 0)
    {
        printf ("clock_gettime on process clock %lx => %s\n",
                (unsigned long int) process_clock, strerror (errno));
        return 1;
    }

    const struct timespec sleeptime = { .tv_nsec = 100000000 };

    struct timespec sleeptimeabs = sleeptime;
    sleeptimeabs.tv_sec += process_before.tv_sec;
    sleeptimeabs.tv_nsec += process_before.tv_nsec;
    while (sleeptimeabs.tv_nsec >= 1000000000)
    {
        ++sleeptimeabs.tv_sec;
        sleeptimeabs.tv_nsec -= 1000000000;
    }
    e = clock_nanosleep (process_clock, TIMER_ABSTIME, &sleeptimeabs, NULL);
    if (e != 0)
    {
        return 1;
    }

    struct timespec process_after;
    if (clock_gettime (process_clock, &process_after) < 0)
    {
        return 1;
    }

    long long sleepdiff = (long long) tsdiff (&sleeptimeabs, &process_after);
    if (sleepdiff < 0)
    {
        printf("absolute clock_nanosleep %llu past target (outside reasonable range)\n",
               sleepdiff);
        return 1;
    }

    unsigned long long int diffabs = tsdiff (&process_before, &process_after);
    if (diffabs < sleeptime.tv_nsec)
    {
        printf ("\
absolute clock_nanosleep slept %llu (outside reasonable range)\n",
                diffabs);
        return 1;
    }

    return result;
}
예제 #26
0
int
main (void)
{
  const char *file;
  int fd;
  struct stat st;
  int result;
  char *inmem;
  char *outmem;
  size_t inlen;
  size_t outlen;

  mtrace ();

  /* Make the content of the file available in memory.  */
  file = "../ChangeLog.8";
  fd = open (file, O_RDONLY);
  if (fd == -1)
    error (EXIT_FAILURE, errno, "cannot open %s", basename (file));

  if (fstat (fd, &st) != 0)
    error (EXIT_FAILURE, errno, "cannot stat %s", basename (file));
  memlen = st.st_size;

  mem = (char *) malloc (memlen + 1);
  if (mem == NULL)
    error (EXIT_FAILURE, errno, "while allocating buffer");

  if (read (fd, mem, memlen) != memlen)
    error (EXIT_FAILURE, 0, "cannot read entire file");
  mem[memlen] = '\0';

  close (fd);

  /* We have to convert a few things from Latin-1 to UTF-8.  */
  cd = iconv_open ("UTF-8", "ISO-8859-1");
  if (cd == (iconv_t) -1)
    error (EXIT_FAILURE, errno, "cannot get conversion descriptor");

  /* For the second test we have to convert the file content to UTF-8.
     Since the text is mostly ASCII it should be enough to allocate
     twice as much memory for the UTF-8 text than for the Latin-1
     text.  */
  umem = (char *) calloc (2, memlen);
  if (umem == NULL)
    error (EXIT_FAILURE, errno, "while allocating buffer");

  inmem = mem;
  inlen = memlen;
  outmem = umem;
  outlen = 2 * memlen - 1;
  iconv (cd, &inmem, &inlen, &outmem, &outlen);
  if (inlen != 0)
    error (EXIT_FAILURE, errno, "cannot convert buffer");

#ifdef _POSIX_CPUTIME
  /* See whether we can use the CPU clock.  */
  use_clock = clock_getcpuclockid (0, &cl) == 0;
#endif

#ifdef DEBUG
  re_set_syntax (RE_DEBUG);
#endif

  /* Run the actual tests.  All tests are run in a single-byte and a
     multi-byte locale.  */
  result = test_expr ("[הבאגיטךםלמסצףעפ�תש�]", 2);
  result |= test_expr ("G.ran", 2);
  result |= test_expr ("G.\\{1\\}ran", 2);
  result |= test_expr ("G.*ran", 3);
  result |= test_expr ("[הבאג]", 0);

  /* Free the resources.  */
  free (umem);
  iconv_close (cd);
  free (mem);

  return result;
}