Exemplo n.º 1
0
void *mon_run() {
	static struct timespec starttime, endtime;
	char buffer[BUFSIZE];
	struct sockaddr saddr;
	if (sock_open()) return 0;
	if (sock_bind(monifname)) return 0;
	state = 1;
	while(state == 1) {
		socklen_t saddr_size = sizeof saddr;
		int size = recvfrom(sock, buffer, BUFSIZE, 0, &saddr, &saddr_size);
		struct wframe * frame = buffertowframe(buffer, size);
		if (frame == NULL)
			continue;
		if(frame->stype == IEEE80211_STYPE_PROBE_REQ)
			clock_gettime(CLOCK_MONOTONIC, &starttime);
		if(frame->stype == IEEE80211_STYPE_PROBE_RESP) {
			clock_gettime(CLOCK_MONOTONIC, &endtime);
			struct timespec ts = tsdiff(starttime, endtime);
			printf("Probe Reponse Time: %ld usec\n", ts.tv_sec * 1000 * 1000 + ts.tv_nsec / 1000);
			state = 2;
		}
		free(frame);
	}
	state = 2;
}
Exemplo n.º 2
0
void
ssrv_t::req_made ()
{
  reqtimes.push_back (sfs_get_tsnow());
  while (reqtimes.size () && 
	 tsdiff (reqtimes[0], sfs_get_tsnow(), ok_amt_lasi))
    reqtimes.pop_front ();
  load_avg = reqtimes.size ();
}
Exemplo n.º 3
0
void
mtd_stats_t::report ()
{
  if (ok_amt_stat_freq == 0 ||
      !tsdiff (start_sample, sfs_get_tsnow(), ok_amt_stat_freq))
    return;
  epoch_t e = new_epoch ();
  warn ("STATS: i=%d;o=%d;r=%d;q=%d;a=%d;t=%d;l=%d\n",
	e.in, e.out, e.rejects, e.queued, e.async_serv, e.to, e.len_msec);
}
Exemplo n.º 4
0
int main(void)
{
	struct timespec ts, ts2;
	unsigned long long diff;

	// test syscall with pointer
	T(syscall(SYS_clock_gettime, CLOCK_REALTIME, &ts));

	// check if timespec is filled correctly
	T(clock_gettime(CLOCK_REALTIME, &ts2));
	// adjust because linux vdso is non-monotonic wrt the syscall..
	ts.tv_nsec += 2;
	diff = tsdiff(ts2, ts);
	if (diff > 5 * 1000000000ULL)
		t_error("large diff between clock_gettime calls: %llu ns\n", diff);

	return t_status;
}
Exemplo n.º 5
0
int main(int argc, char *argv[])
{
	char buffer[BUFSIZE];
	struct sockaddr saddr;
	int opt;
	struct timespec starttime, endtime;
	clock_gettime(CLOCK_MONOTONIC, &starttime);

	unsigned char bcast[] = "\xFF\xFF\xFF\xFF\xFF\xFF";
	sta_add(&sta_head, ccolor++, bcast);
	signal(SIGINT, intHandler);

	while ((opt = getopt(argc, argv, PARAMS)) != -1)
	{
		switch (opt)
		{
			case 'h':
				fprintf(stdout, HELP, argv[0]);
				exit(EXIT_SUCCESS);
			case 'b':
				opt_nobeacon = true;
				break;
			case 'c':
				opt_color = true;
				break;
			case 'd':
				opt_diffstamp = true;
				break;
			case 'm':
				opt_nomgmt = true;
				break;
			case 's':
				opt_simpleaddr = true;
				break;
			case 't':
				opt_timestamp = true;
				break;
			default:
				fprintf(stderr, USAGE, argv[0]);
				exit(EXIT_FAILURE);
		}
	}

	if (optind >= argc)
	{
		fprintf(stderr, USAGE, argv[0]);
		exit(EXIT_FAILURE);
	}

	char * iface = argv[optind];
	int macs = argc - optind - 1;
	if(macs > 0)
	{
		maclist = (unsigned char*)malloc(6*macs);
		unsigned char * pos = maclist;
		for(int i = optind + 1; i < argc; i++)
		{
			unsigned int iMac[6];
			unsigned char mac[6];

			sscanf(argv[i], "%x:%x:%x:%x:%x:%x", &iMac[0], &iMac[1], &iMac[2], &iMac[3], &iMac[4], &iMac[5]);
			for(int j=0;j<6;j++)
				mac[j] = (unsigned char)iMac[j];
			memcpy(pos, mac, 6);
			pos += 6;
		}
	}
	maclist_count = macs;

	if (sock_open()) return 0;
	if (sock_bind(argv[optind])) return 0;
	while(keepRunning) {
		socklen_t saddr_size = sizeof saddr;
		int size = recvfrom(sock, buffer, BUFSIZE, 0, &saddr, &saddr_size);
		analyze(buffer, size);
	}
	clock_gettime(CLOCK_MONOTONIC, &endtime);
	printf(CNORMAL);
	sock_close();
	printf("Station List: \n");
	print_stalist(sta_head->next);
	struct timespec ts = tsdiff(starttime, endtime);
	printf("Total Running Time: %ld.%lds\n", ts.tv_sec, ts.tv_nsec / 1000);
	return 0;
}
Exemplo n.º 6
0
int main(int argc, char **argv) {
    lua_State *L;
    unsigned i, it;
    int c;
    unsigned long n;
    char *p;

    unsigned iters = 1000;
    param_type_t param_type = PARAM_UNSPEC;
    unsigned num_params = 0;
    typedef struct {
	size_t  size;
	char    *buf;
	xsbuf_t *xsbuf;
    } param_t;
    param_t params[MAXPARAM] = {{ 0 }};
    size_t max_param_size = 0;

    struct timespec real_start, real_end, proc_start, proc_end;
    double totalmem;
    int maxmem;

    while ((c = getopt(argc, argv, "i:" "x:" "LMSs:")) != EOF) {
	switch (c) {
	case 'i':
	    iters = strtoul(optarg, &p, 10);
	    if (*p) die("invalid iter count: -i%s\n", optarg);
	    break;
	case 'x':
	    n = strtoul(optarg, &p, 10);
	    if (*p || n > 1) die("invalid string param type (0/1): -x%s\n", optarg);
	    param_type = (param_type_t)n;
	    break;

	case 'L': n = LARGE;  goto add_param;
	case 'M': n = MEDIUM; goto add_param;
	case 'S': n = SMALL;  goto add_param;
	case 's':
	    n = strtoul(optarg, &p, 0);
	    if (*p == 'k') {
		n *= 1024;
		++p;
	    }
	    else if (*p == 'm') {
		n *= 1024 * 1024;
		++p;
	    }
	    if (*p || n < MINPARAMSIZE)
		die("invalid param size: -s%s\n", optarg);
	add_param:
	    if (num_params >= MAXPARAM)
		die("too many parameters; increase MAXPARAM\n");
	    if (!(p = calloc(n, 1)))
		die("Unable to allocate %lu for parameter %u\n", n, num_params + 1);
	    params[num_params].size = n;
	    params[num_params].buf  = p;
	    ++num_params;
	    if (n > max_param_size)
		max_param_size = n;
	    break;
	}
    }
    if (param_type < 0) die("must specify string param type (-x0 or -x1)\n");
    if (!num_params)    die("must specify at least one parameter size\n");

    printf("Test info:\n"
	   "  %u iterations\n"
	   "  %u %s strings: ",
	   iters,
	   num_params,
	   param_type ? "EXTERNAL" : "LUA");
    for (i = 0; i < num_params; ++i) {
	const size_t s = params[i].size;
	if (s >= 1024 && !(s & 1023))
	    printf(" %zuk", s / 1024);
	else
	    printf(" %zu", s);
    }
    printf("\n");

    /*
     * set up Lua environ
     */

    L = luaL_newstate();
    assert(L);
    luaL_openlibs(L);

    if (luaL_dofile(L, "xsperf.lua"))
	die("Can't load xsperf.lua: %s\n", lua_tostring(L, -1));
    lua_settop(L, 0);

    /*
     * run the test
     */

    srand48(getpid());

    lua_checkstack(L, 2 + num_params);

    if (clock_gettime(CLOCK_REALTIME, &real_start))
	die("Can't get starting real time: %s\n", strerror(errno));
    if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &proc_start))
	die("Can't get starting process time: %s\n", strerror(errno));

    totalmem = 0;
    maxmem = 0;

    for (it = 0; it < iters; ++it) {
	xsbuf_t *b;
	int mem;

	lua_getglobal(L, "test_iter");

	for (i = 0; i < num_params; ++i) {
	    const param_t *par = &params[i];

	    /* modify all the param strings to defeat hashing */
	    snprintf(par->buf, par->size, "param %2u: %lX", i + 1, (unsigned long)lrand48());

	    switch (param_type) {
	    case PARAM_LUASTRING:
		lua_pushlstring(L, par->buf, par->size);
		break;
	    case PARAM_XSTRING:
		b = xsbuf_new(par->buf, par->size);
		params[i].xsbuf = b;
		xstring_new(L, b);
		break;
	    case PARAM_UNSPEC: ; /* silence gcc warning */
	    }
	}

	if (lua_pcall(L, num_params, 0, 0))
	    die("Call #%u to test_iter() failed: %s\n", it + 1, lua_tostring(L, -1));

	if (param_type == PARAM_XSTRING) {
	    for (i = 0; i < num_params; ++i) {
		xsbuf_kill(   params[i].xsbuf);
		xsbuf_discard(params[i].xsbuf);
	    }
	}

	mem = lua_gc(L, LUA_GCCOUNT, 0);
	totalmem += mem;
	if (mem > maxmem)
	    maxmem = mem;
    }

    lua_gc(L, LUA_GCCOLLECT, 0);

    if (clock_gettime(CLOCK_REALTIME, &real_end))
	die("Can't get ending real time: %s\n", strerror(errno));
    if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &proc_end))
	die("Can't get ending process time: %s\n", strerror(errno));

    for (i = 0; i < num_params; ++i)
	free(params[i].buf);

    lua_close(L);

    printf("Done!\n");
    printf("User time:   %.3f\n", tsdiff(proc_start, proc_end));
    printf("Clock time:  %.3f\n", tsdiff(real_start, real_end));
    printf("Average mem: %.0f KiB\n", totalmem / iters);
    printf("Maximum mem: %d KiB\n", maxmem);

    return 0;
}
Exemplo n.º 7
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;
}
Exemplo n.º 8
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;
}

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;
}