コード例 #1
0
ファイル: nanosleep.c プロジェクト: SylvestreG/bitrig
int
time_elapsed_with_signal(void)
{
	struct timespec ts, rts;
	struct timeval stv, etv;
	pid_t pid;
	int status;

	signal(SIGUSR1, sighandler);

	pid = getpid();

	switch(fork()) {
	case -1:
		err(1, "fork");
	default:
		ts.tv_sec = 1;
		ts.tv_nsec = 0;
		nanosleep(&ts, NULL);
		kill(pid, SIGUSR1);
		exit(0);
	}

	ts.tv_sec = 10;
	ts.tv_nsec = 0;
	rts.tv_sec = 0;
	rts.tv_nsec = 0;

	if (gettimeofday(&stv, NULL) < 0) {
		warn("gettimeofday");
		return 1;
	}

	if (nanosleep(&ts, &rts) == 0) {
		warnx("nanosleep");
		return 1;
	}

	if (gettimeofday(&etv, NULL) < 0) {
		warn("gettimeofday");
		return 1;
	}

	timersub(&etv, &stv, &stv);

	etv.tv_sec = rts.tv_sec;
	etv.tv_usec = rts.tv_nsec / 1000 + 1; /* the '+ 1' is a "roundup" */

	timeradd(&etv, &stv, &stv);

	if (stv.tv_sec < 10) {
		warnx("slept time + leftover time < 10 sec");
		return 1;
	}


	if (wait(&status) < 0)
		err(1, "wait");

	return 0;
}
コード例 #2
0
ファイル: perf.c プロジェクト: simatic/TrainsProtocol
/* Thread taking care of respecting the times of the experiment. */
void *timeKeeper(void *null){
  int rc;
  t_counters countersBegin, countersEnd;
  struct rusage rusageBegin, rusageEnd;
  struct timeval timeBegin, timeEnd;
  struct timeval startSomme, stopSomme, diffCPU, diffTimeval;

  // Warm-up phase
  usleep(warmup * 1000000);

  // Measurement phase
  if (gettimeofday(&timeBegin, NULL ) < 0)
    ERROR_AT_LINE(EXIT_FAILURE, errno, __FILE__, __LINE__, "gettimeofday");
  if (getrusage(RUSAGE_SELF, &rusageBegin) < 0)
    ERROR_AT_LINE(EXIT_FAILURE, errno, __FILE__, __LINE__, "getrusage");
  countersBegin = counters;

  usleep(measurement * 1000000);

  if (gettimeofday(&timeEnd, NULL ) < 0)
    ERROR_AT_LINE(EXIT_FAILURE, errno, __FILE__, __LINE__, "gettimeofday");
  if (getrusage(RUSAGE_SELF, &rusageEnd) < 0)
    ERROR_AT_LINE(EXIT_FAILURE, errno, __FILE__, __LINE__, "getrusage");
  countersEnd = counters;

  measurementDone = true;

  // Cool-down phase
  usleep(cooldown * 1000000);

  // We display the results
  printf(
      "%s --broadcasters %d --cooldown %d --wagonMaxLen %d --measurement %d --number %d --size %d --trainsNumber %d  --warmup %d\n",
      programName, broadcasters, cooldown, alternateMaxWagonLen, measurement, number, size,
      trainsNumber, warmup);

  printDiffTimeval("time for tr_init (in sec)", timeTrInitEnd, timeTrInitBegin);

  printDiffTimeval("elapsed time (in sec)", timeEnd, timeBegin);

  printDiffTimeval("ru_utime (in sec)", rusageEnd.ru_utime,
      rusageBegin.ru_utime);
  printDiffTimeval("ru_stime (in sec)", rusageEnd.ru_stime,
      rusageBegin.ru_stime);

  timeradd(&rusageBegin.ru_utime, &rusageBegin.ru_stime, &startSomme);
  timeradd(&rusageEnd.ru_utime, &rusageEnd.ru_stime, &stopSomme);
  printDiffTimeval("ru_utime+ru_stime (in sec)", stopSomme, startSomme);

  printf("number of messages delivered to the application ; %llu\n",
      countersEnd.messages_delivered - countersBegin.messages_delivered);
  printf("number of bytes delivered to the application ; %llu\n",
      countersEnd.messages_bytes_delivered
          - countersBegin.messages_bytes_delivered);
  printf("number of bytes of trains received from the network ; %llu\n",
      countersEnd.trains_bytes_received - countersBegin.trains_bytes_received);
  printf("number of trains received from the network ; %llu\n",
      countersEnd.trains_received - countersBegin.trains_received);
  printf("number of bytes of recent trains received from the network ; %llu\n",
      countersEnd.recent_trains_bytes_received
          - countersBegin.recent_trains_bytes_received);
  printf("number of recent trains received from the network ; %llu\n",
      countersEnd.recent_trains_received
          - countersBegin.recent_trains_received);
  printf("number of wagons delivered to the application ; %llu\n",
      countersEnd.wagons_delivered - countersBegin.wagons_delivered);
  printf("number of times automaton has been in state WAIT ; %llu\n",
      countersEnd.wait_states - countersBegin.wait_states);
  printf("number of calls to commRead() ; %llu\n",
      countersEnd.comm_read - countersBegin.comm_read);
  printf("number of bytes read by commRead() calls ; %llu\n",
      countersEnd.comm_read_bytes - countersBegin.comm_read_bytes);
  printf("number of calls to commReadFully() ; %llu\n",
      countersEnd.comm_readFully - countersBegin.comm_readFully);
  printf("number of bytes read by commReadFully() calls ; %llu\n",
      countersEnd.comm_readFully_bytes - countersBegin.comm_readFully_bytes);
  printf("number of calls to commWrite() ; %llu\n",
      countersEnd.comm_write - countersBegin.comm_write);
  printf("number of bytes written by commWrite() calls ; %llu\n",
      countersEnd.comm_write_bytes - countersBegin.comm_write_bytes);
  printf("number of calls to commWritev() ; %llu\n",
      countersEnd.comm_writev - countersBegin.comm_writev);
  printf("number of bytes written by commWritev() calls ; %llu\n",
      countersEnd.comm_writev_bytes - countersBegin.comm_writev_bytes);
  printf("number of calls to newmsg() ; %llu\n",
      countersEnd.newmsg - countersBegin.newmsg);
  printf(
      "number of times there was flow control when calling newmsg() ; %llu\n",
      countersEnd.flowControl - countersBegin.flowControl);

  timersub(&stopSomme, &startSomme, &diffCPU);
  timersub(&timeEnd, &timeBegin, &diffTimeval);
  printf(
      "Broadcasters / number / size / ntr / Average number of delivered wagons per recent train received / Average number of msg per wagon / Throughput of o-broadcasts in Mbps / %%CPU ; %d ; %d ; %d ; %d ; %g ; %g ; %g ; %g\n",
      broadcasters, number, size, ntr,
      ((double) (countersEnd.wagons_delivered - countersBegin.wagons_delivered))
          / ((double) (countersEnd.recent_trains_received
              - countersBegin.recent_trains_received)),
      ((double) (countersEnd.messages_delivered
          - countersBegin.messages_delivered))
          / ((double) (countersEnd.wagons_delivered
              - countersBegin.wagons_delivered)),
      ((double) (countersEnd.messages_bytes_delivered
          - countersBegin.messages_bytes_delivered) * 8)
          / ((double) (diffTimeval.tv_sec * 1000000 + diffTimeval.tv_usec)),
      ((double) (diffCPU.tv_sec * 1000000 + diffCPU.tv_usec)
          / (double) (diffTimeval.tv_sec * 1000000 + diffTimeval.tv_usec)));

  // Termination phase
  rc = trTerminate();
  if (rc < 0) {
    trError_at_line(rc, trErrno, __FILE__, __LINE__, "tr_init()");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);

  return NULL ;
}
コード例 #3
0
ファイル: timer.c プロジェクト: 0x1abin/LinuxLearn
void countdown(Timer* timer, unsigned int timeout) {
	struct timeval now;
	gettimeofday(&now, NULL);
	struct timeval interval = { timeout, 0 };
	timeradd(&now, &interval, &timer->end_time);
}
コード例 #4
0
ファイル: linda.c プロジェクト: Sacharc/UXP1A
bool vlinda_in_generic_unsafe(bool to_remove, struct timeval timeout, const char * match_string, va_list * v_init)
{
	int tuple_index = -1;
	
	//Time now
	struct timeval now;
	gettimeofday(&now, NULL);
	
	//Timestamp end of waiting
	struct timeval timeout_end_timeval; //After that, its end of searching
	timeradd(&now, &timeout, &timeout_end_timeval);
	
	while(true)
	{
		//Check if matching tuple was found
		tuple_index = extract_tuple_from_shmem(match_string);
		if(tuple_index != -1)
			break;
		
		//If not - we are waiting, if timeout ends - we are not waiting anymore
		time_t timeout_timespec_sec = timeout_end_timeval.tv_sec;
		
		//Modify clock, not more than 10^9 nsec
		unsigned long timeout_timespec_nsec = timeout_end_timeval.tv_usec * 1000;
		timeout_timespec_sec += (timeout_timespec_nsec / (1000 * 1000 * 1000));
		timeout_timespec_nsec %= (1000 * 1000 * 1000);
		
		struct timespec timeout_timespec = {timeout_timespec_sec, timeout_timespec_nsec};
		int wait_result = pthread_cond_timedwait(&linda_memory->output_cond, &linda_memory->mem_mutex, &timeout_timespec);
		if(wait_result != 0)
		{
			if(wait_result == ETIMEDOUT)
			{
				break;
			}
			
			printf("pthread_cond_timedwait(): %d\n", wait_result);
			if(linda_logging)
				syslog(3, "pthread_cond_timedwait(): %d", errno);
			return false;
		}
	}

	if(tuple_index == -1)
	{
		return false;
	}

	//Tuple is returned by extract_tuple_from_shmem, so its validate with va_list arguments.
	const struct tuple *found_tuple = linda_memory->first_tuple + tuple_index;
	const size_t info_string_length = strlen(found_tuple->tuple_content);

	size_t info_string_position = 0;
	size_t tuple_position = info_string_length + 1;

	va_list va_read;
	va_copy(va_read, *v_init);

	//Memcpy for arguments in va_list
	while (found_tuple->tuple_content[info_string_position] != 0)
	{
		switch (found_tuple->tuple_content[info_string_position])
		{
			case 'i':
			{
				memcpy(va_arg(v_init, int *), &found_tuple->tuple_content[0] + tuple_position, sizeof(int));
				tuple_position += sizeof(int);
				break;
			}
			case 'f':
			{
				memcpy(va_arg(v_init, double *), &found_tuple->tuple_content[0] + tuple_position, sizeof(double));
				tuple_position += sizeof(double);
				break;
			}
			case 's':
			{
				const size_t string_length = strlen(&found_tuple->tuple_content[0] + tuple_position);
				memcpy(va_arg(v_init, char *), &found_tuple->tuple_content[0] + tuple_position, string_length + 1);
				tuple_position += string_length + 1;
				break;
			}
			default:
			{
				printf("Unknown character in info_string: `%c` (%d)", found_tuple->tuple_content[info_string_position],
					   found_tuple->tuple_content[info_string_position]);
				break;
			}
		}
		++info_string_position;
	}

	va_end(va_read);

	if(to_remove)
	{
		//Delete tuple by replacing it and decrementing tuple_count.
		memcpy(&linda_memory->first_tuple[tuple_index], &linda_memory->first_tuple[tuple_index + 1], (--linda_memory->tuple_count - tuple_index) * sizeof(struct tuple));
		
		if(linda_logging)
			syslog(6, "Removed tuple");
	}
	return true;
}
コード例 #5
0
ファイル: stencil-kernels.c プロジェクト: alucas/StarPU
/*
 * cl_update (CUDA version)
 */
static void update_func_cuda(void *descr[], void *arg)
{
    struct block_description *block = arg;
    int workerid = starpu_worker_get_id();
    DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
    if (block->bz == 0)
        fprintf(stderr,"!!! DO update_func_cuda z %d CUDA%d !!!\n", block->bz, workerid);
    else
        DEBUG( "!!! DO update_func_cuda z %d CUDA%d !!!\n", block->bz, workerid);
#ifdef STARPU_USE_MPI
    int rank = 0;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    DEBUG( "!!!           RANK %d              !!!\n", rank);
#endif
    DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");

    unsigned block_size_z = get_block_size(block->bz);
    unsigned i;
    update_per_worker[workerid]++;

    struct timeval tv, tv2, diff, delta = {.tv_sec = 0, .tv_usec = get_ticks()*1000};
    gettimeofday(&tv, NULL);
    timersub(&tv, &start, &tv2);
    timersub(&tv2, &last_tick[block->bz], &diff);
    while (timercmp(&diff, &delta, >=)) {
        timeradd(&last_tick[block->bz], &delta, &last_tick[block->bz]);
        timersub(&tv2, &last_tick[block->bz], &diff);
        if (who_runs_what_index[block->bz] < who_runs_what_len)
            who_runs_what[block->bz + (who_runs_what_index[block->bz]++) * get_nbz()] = -1;
    }

    if (who_runs_what_index[block->bz] < who_runs_what_len)
        who_runs_what[block->bz + (who_runs_what_index[block->bz]++) * get_nbz()] = global_workerid(workerid);

    /*
     *	Load neighbours' boundaries : TOP
     */

    /* The offset along the z axis is (block_size_z + K) */
    load_subblock_from_buffer_cuda(descr[0], descr[2], block_size_z+K);
    load_subblock_from_buffer_cuda(descr[1], descr[3], block_size_z+K);

    /*
     *	Load neighbours' boundaries : BOTTOM
     */
    load_subblock_from_buffer_cuda(descr[0], descr[4], 0);
    load_subblock_from_buffer_cuda(descr[1], descr[5], 0);

    /*
     *	Stencils ... do the actual work here :) TODO
     */

    for (i=1; i<=K; i++)
    {
        starpu_block_interface_t *oldb = descr[i%2], *newb = descr[(i+1)%2];
        TYPE *old = (void*) oldb->ptr, *new = (void*) newb->ptr;

        /* Shadow data */
        cuda_shadow_host(block->bz, old, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);

        /* And perform actual computation */
#ifdef LIFE
        cuda_life_update_host(block->bz, old, new, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
#else
        cudaMemcpy(new, old, oldb->nx * oldb->ny * oldb->nz * sizeof(*new), cudaMemcpyDeviceToDevice);
#endif /* LIFE */
    }

    cudaError_t cures;
    if ((cures = cudaThreadSynchronize()) != cudaSuccess)
        STARPU_CUDA_REPORT_ERROR(cures);

}
#endif /* STARPU_USE_CUDA */

/*
 * cl_update (CPU version)
 */
static void update_func_cpu(void *descr[], void *arg)
{
    struct block_description *block = arg;
    int workerid = starpu_worker_get_id();
    DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
    if (block->bz == 0)
        fprintf(stderr,"!!! DO update_func_cpu z %d CPU%d !!!\n", block->bz, workerid);
    else
        DEBUG( "!!! DO update_func_cpu z %d CPU%d !!!\n", block->bz, workerid);
#ifdef STARPU_USE_MPI
    int rank = 0;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    DEBUG( "!!!           RANK %d            !!!\n", rank);
#endif
    DEBUG( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");

    unsigned block_size_z = get_block_size(block->bz);
    unsigned i;
    update_per_worker[workerid]++;

    struct timeval tv, tv2, diff, delta = {.tv_sec = 0, .tv_usec = get_ticks() * 1000};
    gettimeofday(&tv, NULL);
    timersub(&tv, &start, &tv2);
    timersub(&tv2, &last_tick[block->bz], &diff);
    while (timercmp(&diff, &delta, >=)) {
        timeradd(&last_tick[block->bz], &delta, &last_tick[block->bz]);
        timersub(&tv2, &last_tick[block->bz], &diff);
        if (who_runs_what_index[block->bz] < who_runs_what_len)
            who_runs_what[block->bz + (who_runs_what_index[block->bz]++) * get_nbz()] = -1;
    }

    if (who_runs_what_index[block->bz] < who_runs_what_len)
        who_runs_what[block->bz + (who_runs_what_index[block->bz]++) * get_nbz()] = global_workerid(workerid);

    /*
     *	Load neighbours' boundaries : TOP
     */

    /* The offset along the z axis is (block_size_z + K) */
    load_subblock_from_buffer_cpu(descr[0], descr[2], block_size_z+K);
    load_subblock_from_buffer_cpu(descr[1], descr[3], block_size_z+K);

    /*
     *	Load neighbours' boundaries : BOTTOM
     */
    load_subblock_from_buffer_cpu(descr[0], descr[4], 0);
    load_subblock_from_buffer_cpu(descr[1], descr[5], 0);

    /*
     *	Stencils ... do the actual work here :) TODO
     */

    for (i=1; i<=K; i++)
    {
        starpu_block_interface_t *oldb = descr[i%2], *newb = descr[(i+1)%2];
        TYPE *old = (void*) oldb->ptr, *new = (void*) newb->ptr;

        /* Shadow data */
        unsigned ldy = oldb->ldy, ldz = oldb->ldz;
        unsigned nx = oldb->nx, ny = oldb->ny, nz = oldb->nz;
        unsigned x, y, z;
        unsigned stepx = 1;
        unsigned stepy = 1;
        unsigned stepz = 1;
        unsigned idx = 0;
        unsigned idy = 0;
        unsigned idz = 0;
        TYPE *ptr = old;

#		include "shadow.h"

        /* And perform actual computation */
#ifdef LIFE
        life_update(block->bz, old, new, oldb->nx, oldb->ny, oldb->nz, oldb->ldy, oldb->ldz, i);
#else
        memcpy(new, old, oldb->nx * oldb->ny * oldb->nz * sizeof(*new));
#endif /* LIFE */
    }
}

/* Performance model and codelet structure */
static struct starpu_perfmodel_t cl_update_model = {
    .type = STARPU_HISTORY_BASED,
    .symbol = "cl_update"
};

starpu_codelet cl_update = {
    .where =
#ifdef STARPU_USE_CUDA
    STARPU_CUDA|
#endif
    STARPU_CPU,
    .cpu_func = update_func_cpu,
#ifdef STARPU_USE_CUDA
    .cuda_func = update_func_cuda,
#endif
    .model = &cl_update_model,
    .nbuffers = 6
};

/*
 * Save the block internal boundaries to give them to our neighbours.
 */

/* CPU version */
static void load_subblock_into_buffer_cpu(starpu_block_interface_t *block,
        starpu_block_interface_t *boundary,
        unsigned firstz)
{
    /* Sanity checks */
    STARPU_ASSERT(block->nx == boundary->nx);
    STARPU_ASSERT(block->ny == boundary->ny);
    STARPU_ASSERT(boundary->nz == K);

    /* NB: this is not fully garanteed ... but it's *very* likely and that
     * makes our life much simpler */
    STARPU_ASSERT(block->ldy == boundary->ldy);
    STARPU_ASSERT(block->ldz == boundary->ldz);

    /* We do a contiguous memory transfer */
    size_t boundary_size = K*block->ldz*block->elemsize;

    unsigned offset = firstz*block->ldz;
    TYPE *block_data = (TYPE *)block->ptr;
    TYPE *boundary_data = (TYPE *)boundary->ptr;
    memcpy(boundary_data, &block_data[offset], boundary_size);
}

/* CUDA version */
#ifdef STARPU_USE_CUDA
static void load_subblock_into_buffer_cuda(starpu_block_interface_t *block,
        starpu_block_interface_t *boundary,
        unsigned firstz)
{
    /* Sanity checks */
    STARPU_ASSERT(block->nx == boundary->nx);
    STARPU_ASSERT(block->ny == boundary->ny);
    STARPU_ASSERT(boundary->nz == K);

    /* NB: this is not fully garanteed ... but it's *very* likely and that
     * makes our life much simpler */
    STARPU_ASSERT(block->ldy == boundary->ldy);
    STARPU_ASSERT(block->ldz == boundary->ldz);

    /* We do a contiguous memory transfer */
    size_t boundary_size = K*block->ldz*block->elemsize;

    unsigned offset = firstz*block->ldz;
    TYPE *block_data = (TYPE *)block->ptr;
    TYPE *boundary_data = (TYPE *)boundary->ptr;
    cudaMemcpy(boundary_data, &block_data[offset], boundary_size, cudaMemcpyDeviceToDevice);
}
#endif /* STARPU_USE_CUDA */

/* Record how many top/bottom saves each worker performed */
unsigned top_per_worker[STARPU_NMAXWORKERS];
unsigned bottom_per_worker[STARPU_NMAXWORKERS];

/* top save, CPU version */
static void dummy_func_top_cpu(void *descr[] __attribute__((unused)), void *arg)
{
    struct block_description *block = arg;
    int workerid = starpu_worker_get_id();
    top_per_worker[workerid]++;

    DEBUG( "DO SAVE Bottom block %d\n", block->bz);

    /* The offset along the z axis is (block_size_z + K)- K */
    unsigned block_size_z = get_block_size(block->bz);

    load_subblock_into_buffer_cpu(descr[0], descr[2], block_size_z);
    load_subblock_into_buffer_cpu(descr[1], descr[3], block_size_z);
}

/* bottom save, CPU version */
static void dummy_func_bottom_cpu(void *descr[] __attribute__((unused)), void *arg)
{
    struct block_description *block = arg;
    int workerid = starpu_worker_get_id();
    bottom_per_worker[workerid]++;

    DEBUG( "DO SAVE Top block %d\n", block->bz);

    load_subblock_into_buffer_cpu(descr[0], descr[2], K);
    load_subblock_into_buffer_cpu(descr[1], descr[3], K);
}

/* top save, CUDA version */
#ifdef STARPU_USE_CUDA
static void dummy_func_top_cuda(void *descr[] __attribute__((unused)), void *arg)
{
    struct block_description *block = arg;
    int workerid = starpu_worker_get_id();
    top_per_worker[workerid]++;

    DEBUG( "DO SAVE Top block %d\n", block->bz);

    /* The offset along the z axis is (block_size_z + K)- K */
    unsigned block_size_z = get_block_size(block->bz);

    load_subblock_into_buffer_cuda(descr[0], descr[2], block_size_z);
    load_subblock_into_buffer_cuda(descr[1], descr[3], block_size_z);
    cudaThreadSynchronize();
}

/* bottom save, CUDA version */
static void dummy_func_bottom_cuda(void *descr[] __attribute__((unused)), void *arg)
{
    struct block_description *block = arg;
    int workerid = starpu_worker_get_id();
    bottom_per_worker[workerid]++;

    DEBUG( "DO SAVE Bottom block %d on CUDA\n", block->bz);

    load_subblock_into_buffer_cuda(descr[0], descr[2], K);
    load_subblock_into_buffer_cuda(descr[1], descr[3], K);
    cudaThreadSynchronize();
}
#endif /* STARPU_USE_CUDA */

/* Performance models and codelet for save */
static struct starpu_perfmodel_t save_cl_bottom_model = {
    .type = STARPU_HISTORY_BASED,
    .symbol = "save_cl_bottom"
};

static struct starpu_perfmodel_t save_cl_top_model = {
    .type = STARPU_HISTORY_BASED,
    .symbol = "save_cl_top"
};

starpu_codelet save_cl_bottom = {
    .where =
#ifdef STARPU_USE_CUDA
    STARPU_CUDA|
#endif
    STARPU_CPU,
    .cpu_func = dummy_func_bottom_cpu,
#ifdef STARPU_USE_CUDA
    .cuda_func = dummy_func_bottom_cuda,
#endif
    .model = &save_cl_bottom_model,
    .nbuffers = 4
};

starpu_codelet save_cl_top = {
    .where =
#ifdef STARPU_USE_CUDA
    STARPU_CUDA|
#endif
    STARPU_CPU,
    .cpu_func = dummy_func_top_cpu,
#ifdef STARPU_USE_CUDA
    .cuda_func = dummy_func_top_cuda,
#endif
    .model = &save_cl_top_model,
    .nbuffers = 4
};
コード例 #6
0
ファイル: udpclient.c プロジェクト: PKRoma/pwnat
int udpclient(int argc, char* argv[])
{
	char* lhost, *lport, *phost, *pport, *rhost, *rport;
	list_t* clients;
	list_t* conn_clients;
	client_t* client;
	client_t* client2;
	socket_t* tcp_serv = NULL;
	socket_t* tcp_sock = NULL;
	socket_t* udp_sock = NULL;
	char data[MSG_MAX_LEN];
	char addrstr[ADDRSTRLEN];
	char pport_s[6];
	struct timeval curr_time;
	struct timeval check_time;
	struct timeval check_interval;
	struct timeval timeout;
	fd_set client_fds;
	fd_set read_fds;
	uint16_t tmp_id;
	uint8_t tmp_type;
	uint16_t tmp_len;
	uint16_t tmp_req_id;
	int num_fds;
	int ret;
	int i;
	int icmp_sock ;
	int timeexc = -1;
	struct sockaddr_in src, dest, rsrc;
	struct hostent* hp;
	uint32_t timeexc_ip;
	signal(SIGINT, &signal_handler);
	i = 0;
	if(index(argv[i], 58) || index(argv[i], 46))
		lhost = argv[i++];
	else
		lhost = NULL;
	lport = argv[i++];
	phost = argv[i++];
	if(index(argv[i], 58) || index(argv[i], 46)) {
		snprintf(pport_s, 5, "2222");
		pport = pport_s;
	} else
		pport = argv[i++];
	rhost = argv[i++];
	rport = argv[i++];
	/* Get info about localhost IP */
	if(!lhost){
		char szHostName[255];
		gethostname(szHostName, 255);
		hp = gethostbyname(szHostName);
	}else{
		hp = gethostbyname(lhost);
	}
	memset(&rsrc, 0, sizeof(struct sockaddr_in));
	timeexc_ip				= *(uint32_t*)hp->h_addr_list[0];
	rsrc.sin_family			= AF_INET;
	rsrc.sin_port			= 0;
	rsrc.sin_addr.s_addr	= timeexc_ip;
	/* IP of destination */
	memset(&src, 0, sizeof(struct sockaddr_in));
	hp					  = gethostbyname(phost);
	timeexc_ip            = *(uint32_t*)hp->h_addr_list[0];
	src.sin_family        = AF_INET;
	src.sin_port          = 0;
	src.sin_addr.s_addr   = timeexc_ip;
	/* IP of where the fake packet (echo request) was going */
	hp = gethostbyname("3.3.3.3");
	memcpy(&dest.sin_addr, hp->h_addr, hp->h_length);
	inet_pton(AF_INET, "3.3.3.3", &(dest.sin_addr));
	srand(time(NULL));
	next_req_id = rand() % 0xffff;
	/* Create an empty list for the clients */
	clients = list_create(sizeof(client_t), p_client_cmp, p_client_copy,
						  p_client_free);
	ERROR_GOTO(clients == NULL, "Error creating clients list.", done);
	/* Create and empty list for the connecting clients */
	conn_clients = list_create(sizeof(client_t), p_client_cmp, p_client_copy,
							   p_client_free);
	ERROR_GOTO(conn_clients == NULL, "Error creating clients list.", done);
	/* Create a TCP server socket to listen for incoming connections */
	tcp_serv = sock_create(lhost, lport, ipver, SOCK_TYPE_TCP, 1, 1);
	ERROR_GOTO(tcp_serv == NULL, "Error creating TCP socket.", done);
	if(debug_level >= DEBUG_LEVEL1) {
		printf("Listening on TCP %s\n",
			   sock_get_str(tcp_serv, addrstr, sizeof(addrstr)));
	}
	FD_ZERO(&client_fds);
	/* Initialize all the timers */
	timerclear(&timeout);
	check_interval.tv_sec = 0;
	check_interval.tv_usec = 500000;
	gettimeofday(&check_time, NULL);
	/* open raw socket */
	create_icmp_socket(&icmp_sock);
	if(icmp_sock == -1) {
		printf("[main] can't open raw socket\n");
		exit(1);
	}
	while(running) {
		if(!timerisset(&timeout))
			timeout.tv_usec = 50000;
		if(++timeexc==100) {
			timeexc=0;
			/* Send ICMP TTL exceeded to penetrate remote NAT */
			send_icmp(icmp_sock, &rsrc, &src, &dest, 0);
		}
		read_fds = client_fds;
		FD_SET(SOCK_FD(tcp_serv), &read_fds);
		ret = select(FD_SETSIZE, &read_fds, NULL, NULL, &timeout);
		PERROR_GOTO(ret < 0, "select", done);
		num_fds = ret;
		gettimeofday(&curr_time, NULL);
		/* Go through all the clients and check if didn't get an ACK for sent
		   data during the timeout period */
		if(timercmp(&curr_time, &check_time, >)) {
			for(i = 0; i < LIST_LEN(clients); i++) {
				client = list_get_at(clients, i);
				ret = client_check_and_resend(client, curr_time);
				if(ret == -2) {
					disconnect_and_remove_client(CLIENT_ID(client), clients,
												 &client_fds);
					i--;
					continue;
				}
				ret = client_check_and_send_keepalive(client, curr_time);
				if(ret == -2) {
					disconnect_and_remove_client(CLIENT_ID(client), clients,
												 &client_fds);
					i--;
				}
			}
			timeradd(&curr_time, &check_interval, &check_time);
		}
		if(num_fds == 0) continue;
		timeexc=0;
		/* Check if pending TCP connection to accept and create a new client
		   and UDP connection if one is ready */
		if(FD_ISSET(SOCK_FD(tcp_serv), &read_fds)) {
			tcp_sock = sock_accept(tcp_serv);
			udp_sock = sock_create(phost, pport, ipver,
								   SOCK_TYPE_UDP, 0, 1);
			client = client_create(next_req_id++, tcp_sock, udp_sock, 1);
			if(!client || !tcp_sock || !udp_sock) {
				if(tcp_sock)
					sock_close(tcp_sock);
				if(udp_sock)
					sock_close(udp_sock);
			} else {
				client2 = list_add(conn_clients, client);
				client_free(client);
				client = NULL;
				client_send_hello(client2, rhost, rport, CLIENT_ID(client2));
				client_add_tcp_fd_to_set(client2, &client_fds);
				client_add_udp_fd_to_set(client2, &client_fds);
			}
			sock_free(tcp_sock);
			sock_free(udp_sock);
			tcp_sock = NULL;
			udp_sock = NULL;
			num_fds--;
		}
		/* Check for pending handshakes from UDP connection */
		for(i = 0; i < LIST_LEN(conn_clients) && num_fds > 0; i++) {
			client = list_get_at(conn_clients, i);
			if(client_udp_fd_isset(client, &read_fds)) {
				num_fds--;
				tmp_req_id = CLIENT_ID(client);
				ret = client_recv_udp_msg(client, data, sizeof(data),
										  &tmp_id, &tmp_type, &tmp_len);
				if(ret == 0)
					ret = handle_message(client, tmp_id, tmp_type,
										 data, tmp_len);
				if(ret < 0) {
					disconnect_and_remove_client(tmp_req_id, conn_clients,
												 &client_fds);
					i--;
				} else {
					client = list_add(clients, client);
					list_delete_at(conn_clients, i);
					client_remove_udp_fd_from_set(client, &read_fds);
					i--;
				}
			}
		}
		/* Check if data is ready from any of the clients */
		for(i = 0; i < LIST_LEN(clients) && num_fds > 0; i++) {
			client = list_get_at(clients, i);
			/* Check for UDP data */
			if(client_udp_fd_isset(client, &read_fds)) {
				num_fds--;
				ret = client_recv_udp_msg(client, data, sizeof(data),
										  &tmp_id, &tmp_type, &tmp_len);
				if(ret == 0)
					ret = handle_message(client, tmp_id, tmp_type,
										 data, tmp_len);
				if(ret < 0) {
					disconnect_and_remove_client(CLIENT_ID(client), clients,
												 &client_fds);
					i--;
					continue; /* Don't go to check the TCP connection */
				}
			}
			/* Check for TCP data */
			if(client_tcp_fd_isset(client, &read_fds)) {
				num_fds--;
				ret = client_recv_tcp_data(client);
				if(ret == 0)
					ret = client_send_udp_data(client);
#if 0 /* if udptunnel is taking up 100% of cpu, try including this */
				else if(ret == 1)
#ifdef _WIN32
					_sleep(1);
#else
					usleep(1000); /* Quick hack so doesn't use 100% of CPU if
                                     data wasn't ready yet (waiting for ack) */
#endif /*WIN32*/
#endif /*0*/
				if(ret < 0) {
					disconnect_and_remove_client(CLIENT_ID(client), clients,
												 &client_fds);
					i--;
				}
			}
		}
	}
done:
	if(debug_level >= DEBUG_LEVEL1)
		printf("Cleaning up...\n");
	if(tcp_serv) {
		sock_close(tcp_serv);
		sock_free(tcp_serv);
	}
	if(udp_sock) {
		sock_close(udp_sock);
		sock_free(udp_sock);
	}
	if(clients)
		list_free(clients);
	if(debug_level >= DEBUG_LEVEL1)
		printf("Goodbye.\n");
	return 0;
}
コード例 #7
0
/*
 * Write out process accounting information, on process exit.
 * Data to be written out is specified in Leffler, et al.
 * and are enumerated below.  (They're also noted in the system
 * "acct.h" header file.)
 */
int
acct_process(struct proc *p)
{
	struct acct acct;
	struct rusage *r;
	struct timeval ut, st, tmp;
	int t;
	struct vnode *vp;
	struct plimit *oplim = NULL;
	int error;

	/* If accounting isn't enabled, don't bother */
	vp = acctp;
	if (vp == NULL)
		return (0);

	/*
	 * Raise the file limit so that accounting can't be stopped by the
	 * user. (XXX - we should think about the cpu limit too).
	 */
	if (p->p_p->ps_limit->p_refcnt > 1) {
		oplim = p->p_p->ps_limit;
		p->p_p->ps_limit = limcopy(p->p_p->ps_limit);
	}
	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;

	/*
	 * Get process accounting information.
	 */

	/* (1) The name of the command that ran */
	bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);

	/* (2) The amount of user and system time that was used */
	calcru(p, &ut, &st, NULL);
	acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
	acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);

	/* (3) The elapsed time the commmand ran (and its starting time) */
	acct.ac_btime = p->p_stats->p_start.tv_sec;
	getmicrotime(&tmp);
	timersub(&tmp, &p->p_stats->p_start, &tmp);
	acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);

	/* (4) The average amount of memory used */
	r = &p->p_stats->p_ru;
	timeradd(&ut, &st, &tmp);
	t = tmp.tv_sec * hz + tmp.tv_usec / tick;
	if (t)
		acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
	else
		acct.ac_mem = 0;

	/* (5) The number of disk I/O operations done */
	acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);

	/* (6) The UID and GID of the process */
	acct.ac_uid = p->p_cred->p_ruid;
	acct.ac_gid = p->p_cred->p_rgid;

	/* (7) The terminal from which the process was started */
	if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
		acct.ac_tty = p->p_pgrp->pg_session->s_ttyp->t_dev;
	else
		acct.ac_tty = NODEV;

	/* (8) The boolean flags that tell how the process terminated, etc. */
	acct.ac_flag = p->p_acflag;

	/*
	 * Now, just write the accounting information to the file.
	 */
	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&acct, sizeof (acct),
	    (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, p->p_ucred, NULL, p);

	if (oplim) {
		limfree(p->p_p->ps_limit);
		p->p_p->ps_limit = oplim;
	}

	return error;
}
コード例 #8
0
ファイル: edf.c プロジェクト: 8l/inferno
int
edfready(Proc *p)
{
	Edf *e;
	Schedq *rq;
	Proc *l, *pp;
	void (*pt)(Proc*, int, vlong);

	if((e = edflock(p)) == nil)
		return 0;
	if (e->d <= now){
		/* past deadline, arrange for next release */
		if ((e->flags & Sporadic) == 0){
			/* Non sporadic processes stay true to their period, calculate next release time */
			while(e->t < now)
				e->t += e->T;
		}	
		if (now < e->t){
			/* Next release is in the future, schedule it */
			if (e->tt == nil || e->tf != releaseintr){
				e->tns = e->t;
				e->tmode = Tabsolute;
				e->tf = releaseintr;
				e->ta = p;
				timeradd(e);
				DPRINT("%t edfready %lud[%s], release=%t\n",
					now, p->pid, statename[p->state], e->t);
			}
			if(p->state == Running && (e->flags & (Yield|Yieldonblock)) == 0 && (e->flags & Extratime)){
				/* If we were running, we've overrun our CPU allocation
				 * or missed the deadline, continue running best-effort at low priority
				 * Otherwise we were blocked.  If we don't yield on block, we continue
				 * best effort
				 */
				DPRINT(">");
				p->pri = PriExtra;
				edfunlock();
				return 0;	/* Stick on runq[PriExtra] */
			}
			DPRINT("%t edfready %lud[%s] wait release at %t\n",
				now, p->pid, statename[p->state], e->t);
			p->state = Waitrelease;
			edfunlock();
			return 1;	/* Make runnable later */
		}
		DPRINT("%t edfready %lud %s release now\n", now, p->pid, statename[p->state]);
		/* release now */
		release(p);
	}
	edfunlock();
	DPRINT("^");
	rq = &runq[PriEdf];
	/* insert in queue in earliest deadline order */
	lock(runq);
	l = nil;
	for(pp = rq->head; pp; pp = pp->rnext){
		if(pp->edf->d > e->d)
			break;
		l = pp;
	}
	p->rnext = pp;
	if (l == nil)
		rq->head = p;
	else
		l->rnext = p;
	if(pp == nil)
		rq->tail = p;
	rq->n++;
	nrdy++;
	runvec |= 1 << PriEdf;
	p->pri = PriEdf;
	p->readytime = m->ticks;
	p->state = Ready;
	unlock(runq);
	if(pt = proctrace)
		pt(p, SReady, now);
	return 1;
}
コード例 #9
0
ファイル: edf.c プロジェクト: 0intro/enhanced-plan9
char *
edfadmit(Proc *p)
{
	char *err;
	Edf *e;
	int i;
	Proc *r;
	void (*pt)(Proc*, int, vlong, vlong);
	long tns;

	e = p->edf;
	if (e->flags & Admitted)
		return "task state";	/* should never happen */

	/* simple sanity checks */
	if (e->T == 0)
		return "T not set";
	if (e->C == 0)
		return "C not set";
	if (e->D > e->T)
		return "D > T";
	if (e->D == 0)	/* if D is not set, set it to T */
		e->D = e->T;
	if (e->C > e->D)
		return "C > D";

	qlock(&edfschedlock);
	if (err = testschedulability(p)){
		qunlock(&edfschedlock);
		return err;
	}
	e->flags |= Admitted;

	edflock(p);

	if(p->trace && (pt = proctrace))
		pt(p, SAdmit, 0, 0);

	/* Look for another proc with the same period to synchronize to */
	for(i=0; (r = psincref(i)) != nil; i++) {
		if(r->state == Dead || r == p){
			psdecref(r);
			continue;
		}
		if (r->edf == nil || (r->edf->flags & Admitted) == 0){
			psdecref(r);
			continue;
		}
		if (r->edf->T == e->T)
			break;
	}
	if (r == nil){
		/* Can't synchronize to another proc, release now */
		e->t = now;
		e->d = 0;
		release(p);
		if (p == up){
			DPRINT("%lud edfadmit self %d[%s], release now: r=%lud d=%lud t=%lud\n",
				now, p->pid, statename[p->state], e->r, e->d, e->t);
			/* We're already running */
			edfrun(p, 1);
		}else{
			/* We're releasing another proc */
			DPRINT("%lud edfadmit other %d[%s], release now: r=%lud d=%lud t=%lud\n",
				now, p->pid, statename[p->state], e->r, e->d, e->t);
			p->ta = p;
			edfunlock();
			qunlock(&edfschedlock);
			releaseintr(nil, p);
			return nil;
		}
	}else{
		/* Release in synch to something else */
		e->t = r->edf->t;
		psdecref(r);
		if (p == up){
			DPRINT("%lud edfadmit self %d[%s], release at %lud\n",
				now, p->pid, statename[p->state], e->t);
		}else{
			DPRINT("%lud edfadmit other %d[%s], release at %lud\n",
				now, p->pid, statename[p->state], e->t);
			if(e->tt == nil){
				e->tf = releaseintr;
				e->ta = p;
				tns = e->t - now;
				if(tns < 20)
					tns = 20;
				e->tns = 1000LL * tns;
				e->tmode = Trelative;
				timeradd(e);
			}
		}
	}
	edfunlock();
	qunlock(&edfschedlock);
	return nil;
}
コード例 #10
0
ファイル: Time.hpp プロジェクト: jlinford/metaprof
 my_type & __AddAssign(my_type const & rhs) {
   timeradd(&value_, &rhs.value_, &value_);
   return *this;
 }
コード例 #11
0
ファイル: rudp.c プロジェクト: AfifFaris/Reliable-UDP
/* Transmit a packet via UDP */
int send_packet(bool_t is_ack, rudp_socket_t rsocket, struct rudp_packet *p, struct sockaddr_in *recipient) {
  char type[5];
  short t=p->header.type;
  if(t == 1)
    strcpy(type, "DATA");
  else if(t == 2)
    strcpy(type, "ACK");
  else if(t == 4)
    strcpy(type, "SYN");
  else if(t == 5)
    strcpy(type, "FIN");
  else
    strcpy(type, "BAD");

  printf("Sending %s packet to %s:%d seq number=%u on socket=%d\n",type, 
       inet_ntoa(recipient->sin_addr), ntohs(recipient->sin_port), p->header.seqno, (int)rsocket);

  if (DROP != 0 && rand() % DROP == 1) {
      printf("Dropped\n");
  }
  else {
    if (sendto((int)rsocket, p, sizeof(struct rudp_packet), 0, (struct sockaddr*)recipient, sizeof(struct sockaddr_in)) < 0) {
      fprintf(stderr, "rudp_sendto: sendto failed\n");
      return -1;
    }
  }

  if(!is_ack) {
    /* Set a timeout event if the packet isn't an ACK */
    struct timeoutargs *timeargs = malloc(sizeof(struct timeoutargs));
    if(timeargs == NULL) {
      fprintf(stderr, "send_packet: Error allocating timeout args\n");
      return -1;
    }
    timeargs->packet = malloc(sizeof(struct rudp_packet));
    if(timeargs->packet == NULL) {
      fprintf(stderr, "send_packet: Error allocating timeout args packet\n");
      return -1;
    }
    timeargs->recipient = malloc(sizeof(struct sockaddr_in));
    if(timeargs->packet == NULL) {
      fprintf(stderr, "send_packet: Error allocating timeout args recipient\n");
      return -1;
    }
    timeargs->fd = rsocket;
    memcpy(timeargs->packet, p, sizeof(struct rudp_packet));
    memcpy(timeargs->recipient, recipient, sizeof(struct sockaddr_in));  
  
    struct timeval currentTime;
    gettimeofday(&currentTime, NULL);
    struct timeval delay;
    delay.tv_sec = RUDP_TIMEOUT/1000;
    delay.tv_usec= 0;
    struct timeval timeout_time;
    timeradd(&currentTime, &delay, &timeout_time);

    struct rudp_socket_list *curr_socket = socket_list_head;
    while(curr_socket != NULL) {
      if(curr_socket->rsock == timeargs->fd) {
        break;
      }
      curr_socket = curr_socket->next;
    }
    if(curr_socket->rsock == timeargs->fd) {
      bool_t session_found = false;
        /* Check if we already have a session for this peer */
        struct session *curr_session = curr_socket->sessions_list_head;
        while(curr_session != NULL) {
          if(compare_sockaddr(&curr_session->address, timeargs->recipient) == 1) {
            /* Found an existing session */
            session_found = true;
            break;
          }
          curr_session = curr_session->next;
        }
        if(session_found) {
          if(timeargs->packet->header.type == RUDP_SYN) {
            curr_session->sender->syn_timeout_arg = timeargs;
          }
          else if(timeargs->packet->header.type == RUDP_FIN) {
            curr_session->sender->fin_timeout_arg = timeargs;
          }
          else if(timeargs->packet->header.type == RUDP_DATA) {
            int i;
            int index;
            for(i = 0; i < RUDP_WINDOW; i++) {
              if(curr_session->sender->sliding_window[i] != NULL && 
                 curr_session->sender->sliding_window[i]->header.seqno == timeargs->packet->header.seqno) {
                index = i;
              }
            }
            curr_session->sender->data_timeout_arg[index] = timeargs;
          }
        }
      }
      event_timeout(timeout_time, timeout_callback, timeargs, "timeout_callback");
  }
  return 0;
}
コード例 #12
0
ファイル: pipe.c プロジェクト: jmatth/homework
int main(int argc, char const* argv[])
{
    int i, j, bsize;
    int ptc[2];
    int ctp[2];
    double avg;
    char *arr;
    pid_t pid;
    struct timeval sum, start, end, timeDiff;

    if (argc != 2)
    {
        fprintf(stderr, "Usage: pipe <size>\n");
        exit(1);
    }

    bsize = atoi(argv[1]);

    sum.tv_sec = 0;
    sum.tv_usec = 0;
    pipe(ptc);
    pipe(ctp);
    pid = fork();
    if (pid > 0)
    {
        close(ptc[0]);
        close(ctp[1]);

        arr = malloc(MB * bsize);
        if (arr == NULL)
        {
            fprintf(stderr, "malloc returned null. X_X\n");
            exit(1);
        }

        for (j = 0; j < MB * bsize; ++j)
        {
            arr[j] = 1;
        }

        for (i = 0; i < ITERS; ++i)
        {
            gettimeofday(&start, NULL);
            write(ptc[1], arr, MB * bsize);
            read(ctp[0], arr, MB * bsize);
            gettimeofday(&end, NULL);

            timersub(&end, &start, &timeDiff);
            timeradd(&sum, &timeDiff, &sum);
        }

        close(ctp[0]);
        close(ptc[1]);
    }
    else if (pid == 0)
    {
        close(ptc[1]);
        close(ctp[0]);

        arr = malloc(MB * bsize);

        if (arr == NULL)
        {
            fprintf(stderr, "malloc returned null. X_X\n");
            exit(1);
        }

        for (i = 0; i < ITERS; ++i)
        {
            read(ptc[0], arr, MB * bsize);
            write(ctp[1], arr, MB * bsize);
        }
        free(arr);
        exit(0);
    }
    else {
        fprintf(stderr, "Problem with fork()! I'll just die now. X_X\n");
        exit(1);
    }

    avg = ((double)sum.tv_sec * 1000000 + (double)sum.tv_usec) / ITERS;
    printf("%d\t%f\n", bsize, avg);
    free(arr);

    return 0;
}
コード例 #13
0
ファイル: test-timers.c プロジェクト: Archer-sys/tor
int
main(int argc, char **argv)
{
  (void)argc;
  (void)argv;
  tor_libevent_cfg cfg;
  memset(&cfg, 0, sizeof(cfg));
  tor_libevent_initialize(&cfg);
  timers_initialize();

  int i;
  int ret;
  struct timeval now;
  tor_gettimeofday(&now);
  monotime_get(&started_at);
  for (i = 0; i < N_TIMERS; ++i) {
    struct timeval delay;
    delay.tv_sec = crypto_rand_int_range(0,MAX_DURATION);
    delay.tv_usec = crypto_rand_int_range(0,1000000);
    delay_usec[i] = delay.tv_sec * 1000000 + delay.tv_usec;
    timeradd(&now, &delay, &fire_at[i]);
    timers[i] = timer_new(timer_cb, &timers[i]);
    timer_schedule(timers[i], &delay);
    ++n_active_timers;
  }

  /* Disable some; we'll make sure they don't trigger. */
  for (i = 0; i < N_DISABLE; ++i) {
    int idx = crypto_rand_int_range(0, N_TIMERS);
    if (is_disabled[idx])
      continue;
    is_disabled[idx] = 1;
    timer_disable(timers[idx]);
    --n_active_timers;
  }

  event_base_loop(tor_libevent_get_base(), 0);

  int64_t total_difference = 0;
  uint64_t total_square_difference = 0;
  tor_assert(n_fired == n_active_timers);
  for (i = 0; i < N_TIMERS; ++i) {
    if (is_disabled[i]) {
      tor_assert(fired[i] == 0);
      continue;
    }
    tor_assert(fired[i] == 1);
    //int64_t diff = difference[i].tv_usec + difference[i].tv_sec * 1000000;
    int64_t diff = diffs_mono_usec[i];
    total_difference += diff;
    total_square_difference += diff*diff;
  }
  const int64_t mean_diff = total_difference / n_active_timers;
  printf("mean difference: "I64_FORMAT" usec\n",
         I64_PRINTF_ARG(mean_diff));

  const double mean_sq = ((double)total_square_difference)/ n_active_timers;
  const double sq_mean = mean_diff * mean_diff;
  const double stddev = sqrt(mean_sq - sq_mean);
  printf("standard deviation: %lf usec\n", stddev);

#define MAX_DIFF_USEC (500*1000)
#define MAX_STDDEV_USEC (500*1000)
#define ODD_DIFF_USEC (2000)
#define ODD_STDDEV_USEC (2000)

  if (mean_diff < 0 || mean_diff > MAX_DIFF_USEC || stddev > MAX_STDDEV_USEC) {
    printf("Either your system is under ridiculous load, or the "
           "timer backend is broken.\n");
    ret = 1;
  } else if (mean_diff > ODD_DIFF_USEC || stddev > ODD_STDDEV_USEC) {
    printf("Either your system is a bit slow or the "
           "timer backend is odd.\n");
    ret = 0;
  } else {
    printf("Looks good enough.\n");
    ret = 0;
  }

  timer_free(NULL);

  for (i = 0; i < N_TIMERS; ++i) {
    timer_free(timers[i]);
  }
  timers_shutdown();
  return ret;
}
コード例 #14
0
ファイル: diag_tty.c プロジェクト: butljon/freediag
/*
 * Use POSIX timers.
 */
ssize_t
diag_tty_read(struct diag_l0_device *dl0d, void *buf, size_t count, int timeout)
{
	ssize_t rv;
	ssize_t n;
	char *p;


#if defined(_POSIX_TIMERS)
	/*
	 * You have to create the timer at startup and then test this code.
	 */
#error "POSIX timer code not finished"
	/* Set our alarm to the timeout:
	 */
	struct itimerspec it;
	timerclear(&it.it_interval);
	timerclear(&it.it_value);

	tv.it_value.tv_sec = timeout / 1000;
	tv.it_value.tv_nsec = (timeout % 1000) * 1000000; 	/* ns */

	dl0d->expired = 0;							/* Clear flag */
	timer_settime(dl0d->timerid, 0, &tv, 0);	/* Arm timer */
#else
	/*
	 * No POSIX timers.  We're going to count on the alarm clock
	 * going off regularly to cause us to time out.
	 */
	struct timeval now, incr, then;

	dl0d->expired = 0;							/* Clear flag */
	(void)gettimeofday(&now, NULL);
	incr.tv_sec = timeout / 1000;
	incr.tv_usec = (timeout % 1000) * 1000;		/* us */
	timeradd(&now, &incr, &then);				/* Expiration time */
#if 0
	fprintf(stderr, "timeout %d now %d:%d incr %d:%d then %d:%d\n",
		timeout,
		now.tv_sec, now.tv_usec,
		incr.tv_sec, incr.tv_usec,
		then.tv_sec, then.tv_usec);
#endif
#endif
	
	errno = 0;
	p = (char *)buf;	/* For easy pointer I/O */
	n = 0;
	rv = 0;

	/* Loop until timeout or we've gotten something. */
	errno = 0;

	while (count > 0 &&
	dl0d->expired == 0 &&
	((rv = read(dl0d->fd,  p + n, count)) >= 0 ||
	(rv == -1 && errno == EINTR))) {
		if (rv == -1) {
			rv = 0;
			errno = 0;
		}
		count -= rv;
		n += rv;
#if !defined(_POSIX_TIMERS)
		(void)gettimeofday(&now, NULL);
		dl0d->expired = timercmp(&now, &then, >);
#if 0
		fprintf(stderr, "now %d:%d\n", now.tv_sec, now.tv_usec);
#endif
#endif
	}

	/*
	 * XXX I'm not exactly sure what we want here.  If we timeout and have
	 * read some characters, do we want to return that?  That's what
	 * I'm doing now.
	 */
	if (rv >= 0) {
		if (n > 0)
			return n;
		else if (dl0d->expired)
			return diag_iseterr(DIAG_ERR_TIMEOUT);
	}

	fprintf(stderr, FLFMT "read on fd %d returned %s.\n",
		FL, dl0d->fd, strerror(errno));

	/* Unspecific Error */
	return diag_iseterr(DIAG_ERR_GENERAL);
}
コード例 #15
0
int pageinout_test(int test_runs, unsigned long long file_size) {
    int fd;
    char tmpname[] = "pageinoutXXXXXX";
    unsigned char *vec;
    int i;
    long long j;
    volatile char *buf;
    int ret = -1;
    int rc;
    struct timeval begin_time, end_time, elapsed_time, total_time_in, total_time_out;
    long pagesize = sysconf(_SC_PAGE_SIZE);

    timerclear(&total_time_in);
    timerclear(&total_time_out);

    fd = create_tmp_file(tmpname, file_size);
    if (fd < 0) {
        return -1;
    }

    vec = alloc_mincore_vec(file_size);
    if (vec == NULL) {
        goto err_alloc;
    }

    buf = mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (buf == ((void *)-1)) {
        fprintf(stderr, "Failed to mmap file: %s\n", strerror(errno));
        goto err_mmap;
    }

    if (!check_caching((void *)buf, vec, file_size, false)) {
        goto err;
    }

    for (i = 0; i < test_runs; i++) {
        gettimeofday(&begin_time, NULL);
        //Read backwards to prevent mmap prefetching
        for (j = ((file_size - 1) & ~(pagesize - 1)); j >= 0; j -= pagesize) {
            buf[j];
        }
        gettimeofday(&end_time, NULL);

        timersub(&end_time, &begin_time, &elapsed_time);
        timeradd(&total_time_in, &elapsed_time, &total_time_in);

        if (!check_caching((void *)buf, vec, file_size, true)) {
            goto err;
        }

        gettimeofday(&begin_time, NULL);
        rc = madvise((void *)buf, file_size, MADV_DONTNEED) ||
               posix_fadvise(fd, 0, file_size, POSIX_FADV_DONTNEED);
        gettimeofday(&end_time, NULL);
        if (rc) {
            fprintf(stderr, "posix_fadvise/madvise DONTNEED failed\n");
            goto err;
        }

        timersub(&end_time, &begin_time, &elapsed_time);
        timeradd(&total_time_out, &elapsed_time, &total_time_out);

        if (!check_caching((void *)buf, vec, file_size, false)) {
            goto err;
        }
    }

    printf("page-in: %llu MB/s\n", (file_size * test_runs * USEC_PER_SEC) /
             (1024 * 1024 * (total_time_in.tv_sec * USEC_PER_SEC + total_time_in.tv_usec)));
    printf("page-out (clean): %llu MB/s\n", (file_size * test_runs * USEC_PER_SEC) /
             (1024 * 1024 * (total_time_out.tv_sec * USEC_PER_SEC + total_time_out.tv_usec)));

    ret = 0;

err:
    munmap((void *)buf, file_size);
err_mmap:
    free(vec);
err_alloc:
    close(fd);
    return ret;
}
コード例 #16
0
ファイル: edf.c プロジェクト: 8l/inferno
char *
edfadmit(Proc *p)
{
	char *err;
	Edf *e;
	int i;
	Proc *r;
	void (*pt)(Proc*, int, vlong);

	e = p->edf;
	if (e->flags & Admitted)
		return "task state";	/* should never happen */

	/* simple sanity checks */
	if (e->T == 0)
		return "T not set";
	if (e->C == 0)
		return "C not set";
	if (e->D > e->T)
		return "D > T";
	if (e->D == 0)	/* if D is not set, set it to T */
		e->D = e->T;
	if (e->C > e->D)
		return "C > D";

	qlock(&edfschedlock);
	if (err = testschedulability(p)){
		qunlock(&edfschedlock);
		return err;
	}
	e->flags |= Admitted;

	edflock(p);

	if(pt = proctrace)
		pt(p, SAdmit, now);

	/* Look for another proc with the same period to synchronize to */
	SET(r);
	for(i=0; i<conf.nproc; i++) {
		r = proctab(i);
		if(r->state == Dead || r == p)
			continue;
		if (r->edf == nil || (r->edf->flags & Admitted) == 0)
			continue;
		if (r->edf->T == e->T)
				break;
	}
	if (i == conf.nproc){
		/* Can't synchronize to another proc, release now */
		e->t = now;
		e->d = 0;
		release(p);
		if (p == up){
			DPRINT("%t edfadmit self %lud[%s], release now: r=%t d=%t t=%t\n",
				now, p->pid, statename[p->state], e->r, e->d, e->t);
			/* We're already running */
			edfrun(p, 1);
		}else{
			/* We're releasing another proc */
			DPRINT("%t edfadmit other %lud[%s], release now: r=%t d=%t t=%t\n",
				now, p->pid, statename[p->state], e->r, e->d, e->t);
			p->ta = p;
			edfunlock();
			qunlock(&edfschedlock);
			releaseintr(nil, p);
			return nil;
		}
	}else{
		/* Release in synch to something else */
		e->t = r->edf->t;
		if (p == up){
			DPRINT("%t edfadmit self %lud[%s], release at %t\n",
				now, p->pid, statename[p->state], e->t);
			edfunlock();
			qunlock(&edfschedlock);
			return nil;
		}else{
			DPRINT("%t edfadmit other %lud[%s], release at %t\n",
				now, p->pid, statename[p->state], e->t);
			if(e->tt == nil){
				e->tf = releaseintr;
				e->ta = p;
				e->tns = e->t;
				e->tmode = Tabsolute;
				timeradd(e);
			}
		}
	}
	edfunlock();
	qunlock(&edfschedlock);
	return nil;
}
コード例 #17
0
JNIEXPORT jdouble JNICALL
Java_sun_management_OperatingSystemImpl_getProcessCpuLoad
(JNIEnv *env, jobject dummy)
{
    // This code is influenced by the darwin top source

    struct task_basic_info_64 task_info_data;
    struct task_thread_times_info thread_info_data;
    struct timeval user_timeval, system_timeval, task_timeval;
    struct timeval now;
    mach_port_t task = mach_task_self();
    kern_return_t kr;

    static jlong last_task_time = 0;
    static jlong last_time      = 0;

    mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT;
    kr = task_info(task,
            TASK_THREAD_TIMES_INFO,
            (task_info_t)&thread_info_data,
            &thread_info_count);
    if (kr != KERN_SUCCESS) {
        // Most likely cause: |task| is a zombie.
        return -1;
    }

    mach_msg_type_number_t count = TASK_BASIC_INFO_64_COUNT;
    kr = task_info(task,
            TASK_BASIC_INFO_64,
            (task_info_t)&task_info_data,
            &count);
    if (kr != KERN_SUCCESS) {
        // Most likely cause: |task| is a zombie.
        return -1;
    }

    /* Set total_time. */
    // thread info contains live time...
    TIME_VALUE_TO_TIMEVAL(&thread_info_data.user_time, &user_timeval);
    TIME_VALUE_TO_TIMEVAL(&thread_info_data.system_time, &system_timeval);
    timeradd(&user_timeval, &system_timeval, &task_timeval);

    // ... task info contains terminated time.
    TIME_VALUE_TO_TIMEVAL(&task_info_data.user_time, &user_timeval);
    TIME_VALUE_TO_TIMEVAL(&task_info_data.system_time, &system_timeval);
    timeradd(&user_timeval, &task_timeval, &task_timeval);
    timeradd(&system_timeval, &task_timeval, &task_timeval);

    if (gettimeofday(&now, NULL) < 0) {
       return -1;
    }
    jint ncpus      = JVM_ActiveProcessorCount();
    jlong time      = TIME_VALUE_TO_MICROSECONDS(now) * ncpus;
    jlong task_time = TIME_VALUE_TO_MICROSECONDS(task_timeval);

    if ((last_task_time == 0) || (last_time == 0)) {
        // First call, just set the last values.
        last_task_time = task_time;
        last_time      = time;
        // return 0 since we have no data, not -1 which indicates error
        return 0;
    }

    jlong task_time_delta = task_time - last_task_time;
    jlong time_delta      = time - last_time;
    if (time_delta == 0) {
        return -1;
    }

    jdouble cpu = (jdouble) task_time_delta / time_delta;

    last_task_time = task_time;
    last_time      = time;

    return cpu;
 }
コード例 #18
0
ファイル: kvm_proc.c プロジェクト: FreeBSDFoundation/freebsd
/*
 * Read proc's from memory file into buffer bp, which has space to hold
 * at most maxcnt procs.
 */
static int
kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p,
    struct kinfo_proc *bp, int maxcnt)
{
	int cnt = 0;
	struct kinfo_proc kinfo_proc, *kp;
	struct pgrp pgrp;
	struct session sess;
	struct cdev t_cdev;
	struct tty tty;
	struct vmspace vmspace;
	struct sigacts sigacts;
#if 0
	struct pstats pstats;
#endif
	struct ucred ucred;
	struct prison pr;
	struct thread mtd;
	struct proc proc;
	struct proc pproc;
	struct sysentvec sysent;
	char svname[KI_EMULNAMELEN];

	kp = &kinfo_proc;
	kp->ki_structsize = sizeof(kinfo_proc);
	/*
	 * Loop on the processes. this is completely broken because we need to be
	 * able to loop on the threads and merge the ones that are the same process some how.
	 */
	for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) {
		memset(kp, 0, sizeof *kp);
		if (KREAD(kd, (u_long)p, &proc)) {
			_kvm_err(kd, kd->program, "can't read proc at %p", p);
			return (-1);
		}
		if (proc.p_state == PRS_NEW)
			continue;
		if (proc.p_state != PRS_ZOMBIE) {
			if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads),
			    &mtd)) {
				_kvm_err(kd, kd->program,
				    "can't read thread at %p",
				    TAILQ_FIRST(&proc.p_threads));
				return (-1);
			}
		}
		if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) {
			kp->ki_ruid = ucred.cr_ruid;
			kp->ki_svuid = ucred.cr_svuid;
			kp->ki_rgid = ucred.cr_rgid;
			kp->ki_svgid = ucred.cr_svgid;
			kp->ki_cr_flags = ucred.cr_flags;
			if (ucred.cr_ngroups > KI_NGROUPS) {
				kp->ki_ngroups = KI_NGROUPS;
				kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
			} else
				kp->ki_ngroups = ucred.cr_ngroups;
			kvm_read(kd, (u_long)ucred.cr_groups, kp->ki_groups,
			    kp->ki_ngroups * sizeof(gid_t));
			kp->ki_uid = ucred.cr_uid;
			if (ucred.cr_prison != NULL) {
				if (KREAD(kd, (u_long)ucred.cr_prison, &pr)) {
					_kvm_err(kd, kd->program,
					    "can't read prison at %p",
					    ucred.cr_prison);
					return (-1);
				}
				kp->ki_jid = pr.pr_id;
			}
		}

		switch(what & ~KERN_PROC_INC_THREAD) {

		case KERN_PROC_GID:
			if (kp->ki_groups[0] != (gid_t)arg)
				continue;
			break;

		case KERN_PROC_PID:
			if (proc.p_pid != (pid_t)arg)
				continue;
			break;

		case KERN_PROC_RGID:
			if (kp->ki_rgid != (gid_t)arg)
				continue;
			break;

		case KERN_PROC_UID:
			if (kp->ki_uid != (uid_t)arg)
				continue;
			break;

		case KERN_PROC_RUID:
			if (kp->ki_ruid != (uid_t)arg)
				continue;
			break;
		}
		/*
		 * We're going to add another proc to the set.  If this
		 * will overflow the buffer, assume the reason is because
		 * nprocs (or the proc list) is corrupt and declare an error.
		 */
		if (cnt >= maxcnt) {
			_kvm_err(kd, kd->program, "nprocs corrupt");
			return (-1);
		}
		/*
		 * gather kinfo_proc
		 */
		kp->ki_paddr = p;
		kp->ki_addr = 0;	/* XXX uarea */
		/* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */
		kp->ki_args = proc.p_args;
		kp->ki_tracep = proc.p_tracevp;
		kp->ki_textvp = proc.p_textvp;
		kp->ki_fd = proc.p_fd;
		kp->ki_vmspace = proc.p_vmspace;
		if (proc.p_sigacts != NULL) {
			if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) {
				_kvm_err(kd, kd->program,
				    "can't read sigacts at %p", proc.p_sigacts);
				return (-1);
			}
			kp->ki_sigignore = sigacts.ps_sigignore;
			kp->ki_sigcatch = sigacts.ps_sigcatch;
		}
#if 0
		if ((proc.p_flag & P_INMEM) && proc.p_stats != NULL) {
			if (KREAD(kd, (u_long)proc.p_stats, &pstats)) {
				_kvm_err(kd, kd->program,
				    "can't read stats at %x", proc.p_stats);
				return (-1);
			}
			kp->ki_start = pstats.p_start;

			/*
			 * XXX: The times here are probably zero and need
			 * to be calculated from the raw data in p_rux and
			 * p_crux.
			 */
			kp->ki_rusage = pstats.p_ru;
			kp->ki_childstime = pstats.p_cru.ru_stime;
			kp->ki_childutime = pstats.p_cru.ru_utime;
			/* Some callers want child-times in a single value */
			timeradd(&kp->ki_childstime, &kp->ki_childutime,
			    &kp->ki_childtime);
		}
#endif
		if (proc.p_oppid)
			kp->ki_ppid = proc.p_oppid;
		else if (proc.p_pptr) {
			if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
				_kvm_err(kd, kd->program,
				    "can't read pproc at %p", proc.p_pptr);
				return (-1);
			}
			kp->ki_ppid = pproc.p_pid;
		} else
			kp->ki_ppid = 0;
		if (proc.p_pgrp == NULL)
			goto nopgrp;
		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
			_kvm_err(kd, kd->program, "can't read pgrp at %p",
				 proc.p_pgrp);
			return (-1);
		}
		kp->ki_pgid = pgrp.pg_id;
		kp->ki_jobc = pgrp.pg_jobc;
		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
			_kvm_err(kd, kd->program, "can't read session at %p",
				pgrp.pg_session);
			return (-1);
		}
		kp->ki_sid = sess.s_sid;
		(void)memcpy(kp->ki_login, sess.s_login,
						sizeof(kp->ki_login));
		kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0;
		if (sess.s_leader == p)
			kp->ki_kiflag |= KI_SLEADER;
		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
				_kvm_err(kd, kd->program,
					 "can't read tty at %p", sess.s_ttyp);
				return (-1);
			}
			if (tty.t_dev != NULL) {
				if (KREAD(kd, (u_long)tty.t_dev, &t_cdev)) {
					_kvm_err(kd, kd->program,
						 "can't read cdev at %p",
						tty.t_dev);
					return (-1);
				}
#if 0
				kp->ki_tdev = t_cdev.si_udev;
#else
				kp->ki_tdev = NODEV;
#endif
			}
			if (tty.t_pgrp != NULL) {
				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
					_kvm_err(kd, kd->program,
						 "can't read tpgrp at %p",
						tty.t_pgrp);
					return (-1);
				}
				kp->ki_tpgid = pgrp.pg_id;
			} else
				kp->ki_tpgid = -1;
			if (tty.t_session != NULL) {
				if (KREAD(kd, (u_long)tty.t_session, &sess)) {
					_kvm_err(kd, kd->program,
					    "can't read session at %p",
					    tty.t_session);
					return (-1);
				}
				kp->ki_tsid = sess.s_sid;
			}
		} else {
nopgrp:
			kp->ki_tdev = NODEV;
		}
		if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg)
			(void)kvm_read(kd, (u_long)mtd.td_wmesg,
			    kp->ki_wmesg, WMESGLEN);

		(void)kvm_read(kd, (u_long)proc.p_vmspace,
		    (char *)&vmspace, sizeof(vmspace));
		kp->ki_size = vmspace.vm_map.size;
		/*
		 * Approximate the kernel's method of calculating
		 * this field.
		 */
#define		pmap_resident_count(pm) ((pm)->pm_stats.resident_count)
		kp->ki_rssize = pmap_resident_count(&vmspace.vm_pmap);
		kp->ki_swrss = vmspace.vm_swrss;
		kp->ki_tsize = vmspace.vm_tsize;
		kp->ki_dsize = vmspace.vm_dsize;
		kp->ki_ssize = vmspace.vm_ssize;

		switch (what & ~KERN_PROC_INC_THREAD) {

		case KERN_PROC_PGRP:
			if (kp->ki_pgid != (pid_t)arg)
				continue;
			break;

		case KERN_PROC_SESSION:
			if (kp->ki_sid != (pid_t)arg)
				continue;
			break;

		case KERN_PROC_TTY:
			if ((proc.p_flag & P_CONTROLT) == 0 ||
			     kp->ki_tdev != (dev_t)arg)
				continue;
			break;
		}
		if (proc.p_comm[0] != 0)
			strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN);
		(void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent,
		    sizeof(sysent));
		(void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname,
		    sizeof(svname));
		if (svname[0] != 0)
			strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN);
		if ((proc.p_state != PRS_ZOMBIE) &&
		    (mtd.td_blocked != 0)) {
			kp->ki_kiflag |= KI_LOCKBLOCK;
			if (mtd.td_lockname)
				(void)kvm_read(kd,
				    (u_long)mtd.td_lockname,
				    kp->ki_lockname, LOCKNAMELEN);
			kp->ki_lockname[LOCKNAMELEN] = 0;
		}
		kp->ki_runtime = cputick2usec(proc.p_rux.rux_runtime);
		kp->ki_pid = proc.p_pid;
		kp->ki_siglist = proc.p_siglist;
		SIGSETOR(kp->ki_siglist, mtd.td_siglist);
		kp->ki_sigmask = mtd.td_sigmask;
		kp->ki_xstat = KW_EXITCODE(proc.p_xexit, proc.p_xsig);
		kp->ki_acflag = proc.p_acflag;
		kp->ki_lock = proc.p_lock;
		if (proc.p_state != PRS_ZOMBIE) {
			kp->ki_swtime = (ticks - proc.p_swtick) / hz;
			kp->ki_flag = proc.p_flag;
			kp->ki_sflag = 0;
			kp->ki_nice = proc.p_nice;
			kp->ki_traceflag = proc.p_traceflag;
			if (proc.p_state == PRS_NORMAL) {
				if (TD_ON_RUNQ(&mtd) ||
				    TD_CAN_RUN(&mtd) ||
				    TD_IS_RUNNING(&mtd)) {
					kp->ki_stat = SRUN;
				} else if (mtd.td_state ==
				    TDS_INHIBITED) {
					if (P_SHOULDSTOP(&proc)) {
						kp->ki_stat = SSTOP;
					} else if (
					    TD_IS_SLEEPING(&mtd)) {
						kp->ki_stat = SSLEEP;
					} else if (TD_ON_LOCK(&mtd)) {
						kp->ki_stat = SLOCK;
					} else {
						kp->ki_stat = SWAIT;
					}
				}
			} else {
				kp->ki_stat = SIDL;
			}
			/* Stuff from the thread */
			kp->ki_pri.pri_level = mtd.td_priority;
			kp->ki_pri.pri_native = mtd.td_base_pri;
			kp->ki_lastcpu = mtd.td_lastcpu;
			kp->ki_wchan = mtd.td_wchan;
			kp->ki_oncpu = mtd.td_oncpu;
			if (mtd.td_name[0] != '\0')
				strlcpy(kp->ki_tdname, mtd.td_name, sizeof(kp->ki_tdname));
			kp->ki_pctcpu = 0;
			kp->ki_rqindex = 0;

			/*
			 * Note: legacy fields; wraps at NO_CPU_OLD or the
			 * old max CPU value as appropriate
			 */
			if (mtd.td_lastcpu == NOCPU)
				kp->ki_lastcpu_old = NOCPU_OLD;
			else if (mtd.td_lastcpu > MAXCPU_OLD)
				kp->ki_lastcpu_old = MAXCPU_OLD;
			else
				kp->ki_lastcpu_old = mtd.td_lastcpu;

			if (mtd.td_oncpu == NOCPU)
				kp->ki_oncpu_old = NOCPU_OLD;
			else if (mtd.td_oncpu > MAXCPU_OLD)
				kp->ki_oncpu_old = MAXCPU_OLD;
			else
				kp->ki_oncpu_old = mtd.td_oncpu;
		} else {
			kp->ki_stat = SZOMB;
		}
		kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
		bcopy(&kinfo_proc, bp, sizeof(kinfo_proc));
		++bp;
		++cnt;
	}
	return (cnt);
}
コード例 #19
0
bool KPtyDevicePrivate::doWait(int msecs, bool reading)
{
    Q_Q(KPtyDevice);
#ifndef __linux__
    struct timeval etv;
#endif
    struct timeval tv, *tvp;

    if (msecs < 0)
        tvp = 0;
    else {
        tv.tv_sec = msecs / 1000;
        tv.tv_usec = (msecs % 1000) * 1000;
#ifndef __linux__
        gettimeofday(&etv, 0);
        timeradd(&tv, &etv, &etv);
#endif
        tvp = &tv;
    }

    while (reading ? readNotifier->isEnabled() : !writeBuffer.isEmpty()) {
        fd_set rfds;
        fd_set wfds;

        FD_ZERO(&rfds);
        FD_ZERO(&wfds);

        if (readNotifier->isEnabled())
            FD_SET(q->masterFd(), &rfds);
        if (!writeBuffer.isEmpty())
            FD_SET(q->masterFd(), &wfds);

#ifndef __linux__
        if (tvp) {
            gettimeofday(&tv, 0);
            timersub(&etv, &tv, &tv);
            if (tv.tv_sec < 0)
                tv.tv_sec = tv.tv_usec = 0;
        }
#endif

        switch (select(q->masterFd() + 1, &rfds, &wfds, 0, tvp)) {
        case -1:
            if (errno == EINTR)
                break;
            return false;
        case 0:
            q->setErrorString(QLatin1String("PTY operation timed out"));
            return false;
        default:
            if (FD_ISSET(q->masterFd(), &rfds)) {
                bool canRead = _k_canRead();
                if (reading && canRead)
                    return true;
            }
            if (FD_ISSET(q->masterFd(), &wfds)) {
                bool canWrite = _k_canWrite();
                if (!reading)
                    return canWrite;
            }
            break;
        }
    }
    return false;
}
コード例 #20
0
ファイル: lzc.c プロジェクト: MarioJC/calibre
static void lz_analyze_block(lz_info *lzi)
{
  int *lentab, *lenp;
  unsigned char **prevtab, **prevp;
  unsigned char *bbp, *bbe;
  unsigned char *chartab[256];
  unsigned char *cursor;
  int prevlen;
  int ch;
  int maxlen;
  long wasinc;
  int max_dist = lzi->max_dist;
#ifdef DEBUG_ANALYZE_BLOCK
  static short n = 0;
#endif
#ifdef DEBUG_PERF
  struct rusage innerloop;
  struct timeval innertime, tmptime;
  struct rusage outerloop;
  struct timeval outertime;
  struct rusage initialloop;
  struct timeval initialtime;
  struct rusage totalloop;
  struct timeval totaltime;
#endif

#ifdef DEBUG_ANALYZE_BLOCK
  fprintf(stderr, "Analyzing block %d, cur_loc = %06x\n", n, lzi->cur_loc);
#endif
  memset(chartab, 0, sizeof(chartab));
  prevtab = prevp = lzi->prevtab;
  lentab = lenp = lzi->lentab;
  memset(prevtab, 0, sizeof(*prevtab) * lzi->chars_in_buf);
  memset(lentab, 0, sizeof(*lentab) * lzi->chars_in_buf);
#ifdef DEBUG_PERF
  memset(&innertime, 0, sizeof(innertime));
  memset(&outertime, 0, sizeof(outertime));
  getrusage(RUSAGE_SELF, &initialloop);
  totalloop = initialloop;
#endif
  bbp = lzi->block_buf;
  bbe = bbp + lzi->chars_in_buf;
  while (bbp < bbe) {
    if (chartab[ch = *bbp]) {
      *prevp = chartab[ch];
      *lenp = 1;
    }
    chartab[ch] = bbp;
    bbp++;
    prevp++;
    lenp++;
  }
#ifdef DEBUG_PERF
  initialtime = initialloop.ru_utime;
  getrusage(RUSAGE_SELF, &initialloop);
  timersub(&initialloop.ru_utime, &initialtime, &initialtime);
#endif
  wasinc = 1;
  for (maxlen = 1; wasinc && (maxlen < lzi->max_match); maxlen++) {
#ifdef DEBUG_PERF
    getrusage(RUSAGE_SELF, &outerloop);
#endif
    bbp = bbe - maxlen - 1;
    lenp = lentab + lzi->chars_in_buf - maxlen - 1;
    prevp = prevtab + lzi->chars_in_buf - maxlen - 1;
    wasinc = 0;
    while (bbp > lzi->block_buf) {
      if (*lenp == maxlen) {
#ifdef DEBUG_PERF
	getrusage(RUSAGE_SELF, &innerloop);
#endif
	ch = bbp[maxlen];
	cursor = *prevp;
	while(cursor && ((bbp - cursor) <= max_dist)) {
	  prevlen = *(cursor - lzi->block_buf + lentab);
	  if (cursor[maxlen] == ch) {
	    *prevp = cursor;
	    (*lenp)++;
	    wasinc++;
	    break;
	  }
	  if (prevlen != maxlen) break;
	  cursor = *(cursor - lzi->block_buf + prevtab);
	}
#ifdef DEBUG_PERF
	tmptime = innerloop.ru_utime;
	getrusage(RUSAGE_SELF, &innerloop);
	timersub(&innerloop.ru_utime, &tmptime, &tmptime);
	timeradd(&tmptime, &innertime, &innertime);
#endif
      }
      bbp--;
      prevp--;
      lenp--;
    }
#ifdef DEBUG_PERF
    tmptime = outerloop.ru_utime;
    getrusage(RUSAGE_SELF, &outerloop);
    timersub(&outerloop.ru_utime, &tmptime, &tmptime);
    timeradd(&tmptime, &outertime, &outertime);
#endif
    //    fprintf(stderr, "maxlen = %d, wasinc = %ld\n", maxlen, wasinc);
  }
#ifdef DEBUG_PERF
  totaltime = totalloop.ru_utime;
  getrusage(RUSAGE_SELF, &totalloop);
  timersub(&totalloop.ru_utime, &totaltime, &totaltime);
  fprintf(stderr, "Time spend in initial loop = %f\n", initialtime.tv_sec + initialtime.tv_usec/(double)1E6);
  fprintf(stderr, "Time spend in outer loop = %f\n", outertime.tv_sec + outertime.tv_usec/(double)1E6);
  fprintf(stderr, "Time spend in inner loop = %f\n", innertime.tv_sec + innertime.tv_usec/(double)1E6);
  fprintf(stderr, "Time spend in all loops = %f\n", totaltime.tv_sec + totaltime.tv_usec/(double)1E6);
#endif
  lzi->analysis_valid = 1;
#ifdef DEBUG_ANALYZE_BLOCK
  fprintf(stderr, "Done analyzing block %d, cur_loc = %06x\n", n++, lzi->cur_loc);
#endif
}
コード例 #21
0
/// Update information about threads count and CPU usage.
/// @param task [in] The port of task for with information is to be reterned.
/// @param tinfo [out] Information was updated with list of threads within given task. 
/// @return Upon successful completion 0 is returned.
static int update_threads_info(task_t task, task_record_t *tinfo)
{
     kern_return_t kr;
     thread_act_port_array_t threads_list;
     mach_msg_type_number_t threads_count, i;
     thread_record_t *thread;

     kr = task_threads(task, &threads_list, &threads_count);
     if (kr != KERN_SUCCESS)
     {
          syslog(LOG_WARNING, "error in task_threads(): %s", mach_error_string(kr));
          return -1;
     }
    
     free_threads_array(tinfo->threads_arr, tinfo->threads);
     free(tinfo->threads_arr);
     tinfo->threads = threads_count;
     tinfo->threads_arr = malloc(sizeof(thread_record_t*)*threads_count);

     for (i = 0; i < threads_count; i++)
     {
          thread_basic_info_data_t mach_thread_info;
          mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;

          thread = calloc(1, sizeof(thread_record_t));
          tinfo->threads_arr[i] = thread;
          
          kr = thread_info(threads_list[i], THREAD_BASIC_INFO, (thread_info_t)&mach_thread_info, &count);
          if (kr != KERN_SUCCESS)
          {
               syslog(LOG_INFO, "error in thread_info(basic_info): %s", mach_error_string(kr));
               continue;
          }

          thread->run_state = mach_thread_info.run_state;
          thread->sleep_time = mach_thread_info.sleep_time;
          thread->suspend_count = mach_thread_info.suspend_count;
          thread->user_time = mach_thread_info.user_time;
          thread->system_time = mach_thread_info.system_time;
          thread->flags = mach_thread_info.flags;

          if ((mach_thread_info.flags & TH_FLAGS_IDLE) == 0)
          {
               struct timeval tv;
               TIME_VALUE_TO_TIMEVAL(&mach_thread_info.user_time, &tv);
               timeradd(&tinfo->time_user, &tv, &tinfo->time_user);
               TIME_VALUE_TO_TIMEVAL(&mach_thread_info.system_time, &tv);
               timeradd(&tinfo->time_kernel, &tv, &tinfo->time_kernel);
          }

          thread_identifier_info_data_t mach_thread_id_info;
          count = THREAD_IDENTIFIER_INFO_COUNT;

          kr = thread_info(threads_list[i], THREAD_IDENTIFIER_INFO, (thread_info_t)&mach_thread_id_info, &count);

          if (kr != KERN_SUCCESS)
          {
               syslog(LOG_INFO, "error in thread_info(id_info): %s", mach_error_string(kr));
               continue;
          }
    
          thread->thread_id = mach_thread_id_info.thread_id;

          kr = mach_port_deallocate(mach_task_self(), threads_list[i]);
          if (kr != KERN_SUCCESS)
          {
               syslog(LOG_INFO, "%s, error in mach_port_deallocate(): ", __FUNCTION__, mach_error_string(kr));
          }
     }

     kr = vm_deallocate(mach_task_self(), (vm_address_t)threads_list, threads_count * sizeof(thread_act_t));

     return 0;
}
コード例 #22
0
ファイル: udpclient.c プロジェクト: eXcomm/mobius
int udpclient(int argc, char *argv[])
{

	list_t *clients = NULL;
	list_t *conn_clients;
	client_t *client;
	client_t *tunnel;
	client_t *client2;

	char data[MSG_MAX_LEN];
	char addrstr[ADDRSTRLEN];
	char taddrstr[ADDRSTRLEN];

	socket_t *tcp_sock = NULL;
	socket_t *udp_sock = NULL;
	socket_t *next_sock = NULL;

	struct timeval curr_time;
	struct timeval check_time;
	struct timeval check_interval;
	struct timeval timeout;
	fd_set client_fds;
	fd_set read_fds;
	uint16_t tmp_id;
	uint8_t tmp_type;
	uint16_t tmp_len;
	// uint16_t tmp_req_id;
	int num_fds;
	uint32_t sourceid;



	int ret;
	int i;

	signal(SIGINT, &signal_handler);

	i = 0;    
	lhost = (argc - i == 5) ? NULL : argv[i++];
	lport = argv[i++];
	rport = argv[i++];
	phost = argv[i++];
	pport = argv[i++];
	relays = atoi(argv[i++]);
	if(debug_level >= DEBUG_LEVEL1)
		printf("relays need %d \n",relays);


	/* Check validity of ports (can't check ip's b/c might be host names) */

	ERROR_GOTO(!isnum(lport), "Invalid listen port.", done);

	ERROR_GOTO(!isnum(rport), "Invalid recv port.", done);

	ERROR_GOTO(!isnum(pport), "Invalid inter port.", done);
	//ERROR_GOTO(!isnum(rport), "Invalid remote port.", done);

	srand(inet_addr(lhost));
	localid=(rand());
	generate_rsakey(lhost);
	if(debug_level >= DEBUG_LEVEL1)
	{
		printf("local id %d \n",localid);
	}
	next_req_id = rand() % 0xffff;

	/* Create an empty list for the clients */
	clients = list_create(sizeof(client_t), p_client_cmp, p_client_copy,
	                      p_client_free, 1);
	ERROR_GOTO(clients == NULL, "Error creating clients list.", done);

	/* Create and empty list for the connecting clients */
	conn_clients = list_create(sizeof(client_t), p_client_cmp, p_client_copy,
	                           p_client_free, 1);
	ERROR_GOTO(conn_clients == NULL, "Error creating conn_clients list.", done);

	relay_clients = list_create(sizeof(client_t), p_client_cmp, p_client_copy,
	                            p_client_free, 1);
	ERROR_GOTO(relay_clients == NULL, "Error creating clients list.", done);

	/* Create a TCP server socket to listen for incoming connections */
	tcp_serv = sock_create(lhost, lport, ipver, SOCK_TYPE_TCP, 1, 1);

	ERROR_GOTO(tcp_serv == NULL, "Error creating TCP socket.", done);
	udp_serv = sock_create(lhost, rport,ipver, SOCK_TYPE_UDP, 1, 1);
	ERROR_GOTO(udp_serv == NULL, "Error creating TCP socket.", done);
	if(debug_level >= DEBUG_LEVEL1)
	{
		printf("Listening on TCP %s,UDP %s \n",
		       sock_get_str(tcp_serv, addrstr, sizeof(addrstr)),sock_get_str(udp_serv, taddrstr, sizeof(taddrstr)));
	}
	next_sock = sock_create(phost, pport, ipver, SOCK_TYPE_UDP, 0, 1);

	msg_send_req(next_sock,lhost,rport,0,localid);	   
	sock_free(next_sock);		
	next_sock = NULL;		

	FD_ZERO(&client_fds);

	/* Initialize all the timers */
	timerclear(&timeout);
	check_interval.tv_sec = 0;
	check_interval.tv_usec = 500000;
	gettimeofday(&check_time, NULL);


	while(running)
	{
		if(!timerisset(&timeout))
			timeout.tv_usec = 50000;

		read_fds = client_fds;
		FD_SET(SOCK_FD(tcp_serv), &read_fds);
		FD_SET(SOCK_FD(udp_serv), &read_fds);

		ret = select(FD_SETSIZE, &read_fds, NULL, NULL, &timeout);
		PERROR_GOTO(ret < 0, "select", done);
		num_fds = ret;

		gettimeofday(&curr_time, NULL);

		/* Go through all the clients and check if didn't get an ACK for sent
		 data during the timeout period */
		if(timercmp(&curr_time, &check_time, >))
		{
			for(i = 0; i < LIST_LEN(clients); i++)
			{
				client = list_get_at(clients, i);

				ret = client_check_and_resend(client, curr_time);
				if(ret == -2)
				{
					disconnect_and_remove_client(CLIENT_ID(client), clients,
					                             &client_fds, 1);
					i--;
					continue;
				}

				ret = client_check_and_send_keepalive(client, curr_time);
				if(ret == -2)
				{
					disconnect_and_remove_client(CLIENT_ID(client), clients,
					                             &client_fds, 1);
					i--;
				}
			}

			timeradd(&curr_time, &check_interval, &check_time);
		}

		if(num_fds == 0)
			continue;

		/* Check if pending TCP connection to accept and create a new client
		 and UDP connection if one is ready */
		if(FD_ISSET(SOCK_FD(tcp_serv), &read_fds))
		{
			tcp_sock = sock_accept(tcp_serv);
			if(tcp_sock == NULL)
				continue;
			if(SelectMethod(tcp_sock->fd)==-1)
			{ 
				if(debug_level >= DEBUG_LEVEL1)
					printf("socks version error\n");
				return-1;
			}
			rhost=ParseCommand(tcp_sock->fd);
			if (0<LIST_LEN(relay_clients))
			{

				tunnel = list_get_at(relay_clients, 0);

				udp_sock =sock_copy(CLIENT_TCP_SOCK(tunnel));

				SOCK_FD(udp_sock)=socket(AF_INET, SOCK_DGRAM, 0); 					
			}

			if(udp_sock == NULL)
			{
				sock_close(tcp_sock);
				sock_free(tcp_sock);
				continue;
			}

			client = client_create(next_req_id++, localid, tcp_sock, udp_sock, 1);
			memcpy(client->rsakey,tunnel->rsakey,strlen(tunnel->rsakey));

			printf("expid rsakey is %s",client->rsakey);
			if(debug_level >= DEBUG_LEVEL1)
				printf("create client id %d \n",CLIENT_ID(client));
			if(!client || !tcp_sock || !udp_sock)
			{
				if(tcp_sock)
					sock_close(tcp_sock);
				if(udp_sock)
					sock_close(udp_sock);
			}
			else
			{
				client2 = list_add(conn_clients, client, 1);
				client_free(client);
				client = NULL;
				if(debug_level >= DEBUG_LEVEL1)
				{
					sock_get_str(CLIENT_TCP_SOCK(client2), addrstr,
					             sizeof(addrstr));				
					printf("tunnel(%d): local %s ",client2->sourceid, addrstr);
					sock_get_str(CLIENT_UDP_SOCK(client2), addrstr,
					             sizeof(addrstr));
					printf("to %s \n",addrstr);
				}
				client_send_hello(client2,rhost,CLIENT_ID(client2));
				client_add_tcp_fd_to_set(client2, &client_fds);
				//client_add_udp_fd_to_set(client2, &client_fds);
			}

			sock_free(tcp_sock);
			sock_free(udp_sock);
			tcp_sock = NULL;
			udp_sock = NULL;

			num_fds--;
		}

		/* Check for UDP data */
		if(FD_ISSET(SOCK_FD(udp_serv), &read_fds))
		{


			//ret = client_recv_udp_msg(client, data, sizeof(data),
			//                          &tmp_id, &tmp_type, &tmp_len,&sourceid);
			ret = msg_recv_msg(udp_serv, data, sizeof(data),
			                   &tmp_id, &tmp_type, &tmp_len,&sourceid);

			if(debug_level >= DEBUG_LEVEL2)
				printf("recv msg from %d type %d %d bytes \n ",sourceid,tmp_type,tmp_len);
			if(ret == 0)
				ret = handle_message(tmp_id, tmp_type,
				                     data, tmp_len,sourceid,clients, conn_clients);
			/*if(ret < 0)
			{

				disconnect_and_remove_client(tmp_id, clients,
				                             &client_fds, 1);

		}	*/		
		}

		/* Check if data is ready from any of the clients */
		for(i = 0; i < LIST_LEN(clients); i++)
		{
			client = list_get_at(clients, i);



			/* Check for TCP data */
			if(num_fds > 0 && client_tcp_fd_isset(client, &read_fds))
			{
				ret = client_recv_tcp_data(client);
				if(ret == -1)
				{
					disconnect_and_remove_client(CLIENT_ID(client), clients,
					                             &client_fds, 1);
					i--;
					continue;
				}
				else if(ret == -2)
				{
					client_mark_to_disconnect(client);
					disconnect_and_remove_client(CLIENT_ID(client),
					                             clients, &client_fds, 0);
				}

				num_fds--;
			}

			/* send any TCP data that was ready */
			ret = client_send_udp_data(client);
			if(ret < 0)
			{
				disconnect_and_remove_client(CLIENT_ID(client), clients,
				                             &client_fds, 1);
				i--;
			}
		}

		/* Finally, send any udp data that's still in the queue */
		for(i = 0; i < LIST_LEN(clients); i++)
		{
			client = list_get_at(clients, i);
			ret = client_send_udp_data(client);

			if(ret < 0 || client_ready_to_disconnect(client))
			{
				disconnect_and_remove_client(CLIENT_ID(client), clients,
				                             &client_fds, 1);
				i--;
			}
		}
	}

	done:
		if(debug_level >= DEBUG_LEVEL1)
			printf("Cleaning up...\n");
		if(tcp_serv)
	{
		sock_close(tcp_serv);
		sock_free(tcp_serv);
	}
		if(udp_serv)
	{
		sock_close(udp_serv);
		sock_free(udp_serv);
	}
		if(clients)
			list_free(clients);
		if(conn_clients)
			list_free(conn_clients);
		if(debug_level >= DEBUG_LEVEL1)
			printf("Goodbye.\n");
		return 0;
}
コード例 #23
0
ファイル: MachTask.cpp プロジェクト: Keno/lldb
std::string
MachTask::GetProfileData (DNBProfileDataScanType scanType)
{
    std::string result;
    
    static int32_t numCPU = -1;
    struct host_cpu_load_info host_info;
    if (scanType & eProfileHostCPU)
    {
        int32_t mib[] = {CTL_HW, HW_AVAILCPU};
        size_t len = sizeof(numCPU);
        if (numCPU == -1)
        {
            if (sysctl(mib, sizeof(mib) / sizeof(int32_t), &numCPU, &len, NULL, 0) != 0)
                return result;
        }
        
        mach_port_t localHost = mach_host_self();
        mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
        kern_return_t kr = host_statistics(localHost, HOST_CPU_LOAD_INFO, (host_info_t)&host_info, &count);
        if (kr != KERN_SUCCESS)
            return result;
    }
    
    task_t task = TaskPort();
    if (task == TASK_NULL)
        return result;
    
    struct task_basic_info task_info;
    DNBError err;
    err = BasicInfo(task, &task_info);
    
    if (!err.Success())
        return result;
    
    uint64_t elapsed_usec = 0;
    uint64_t task_used_usec = 0;
    if (scanType & eProfileCPU)
    {
        // Get current used time.
        struct timeval current_used_time;
        struct timeval tv;
        TIME_VALUE_TO_TIMEVAL(&task_info.user_time, &current_used_time);
        TIME_VALUE_TO_TIMEVAL(&task_info.system_time, &tv);
        timeradd(&current_used_time, &tv, &current_used_time);
        task_used_usec = current_used_time.tv_sec * 1000000ULL + current_used_time.tv_usec;
        
        struct timeval current_elapsed_time;
        int res = gettimeofday(&current_elapsed_time, NULL);
        if (res == 0)
        {
            elapsed_usec = current_elapsed_time.tv_sec * 1000000ULL + current_elapsed_time.tv_usec;
        }
    }
    
    std::vector<uint64_t> threads_id;
    std::vector<std::string> threads_name;
    std::vector<uint64_t> threads_used_usec;

    if (scanType & eProfileThreadsCPU)
    {
        get_threads_profile_data(scanType, task, m_process->ProcessID(), threads_id, threads_name, threads_used_usec);
    }
    
    struct vm_statistics vm_stats;
    uint64_t physical_memory;
    mach_vm_size_t rprvt = 0;
    mach_vm_size_t rsize = 0;
    mach_vm_size_t vprvt = 0;
    mach_vm_size_t vsize = 0;
    mach_vm_size_t dirty_size = 0;
    mach_vm_size_t purgeable = 0;
    mach_vm_size_t anonymous = 0;
    if (m_vm_memory.GetMemoryProfile(scanType, task, task_info, m_process->GetCPUType(), m_process->ProcessID(), vm_stats, physical_memory, rprvt, rsize, vprvt, vsize, dirty_size, purgeable, anonymous))
    {
        std::ostringstream profile_data_stream;
        
        if (scanType & eProfileHostCPU)
        {
            profile_data_stream << "num_cpu:" << numCPU << ';';
            profile_data_stream << "host_user_ticks:" << host_info.cpu_ticks[CPU_STATE_USER] << ';';
            profile_data_stream << "host_sys_ticks:" << host_info.cpu_ticks[CPU_STATE_SYSTEM] << ';';
            profile_data_stream << "host_idle_ticks:" << host_info.cpu_ticks[CPU_STATE_IDLE] << ';';
        }
        
        if (scanType & eProfileCPU)
        {
            profile_data_stream << "elapsed_usec:" << elapsed_usec << ';';
            profile_data_stream << "task_used_usec:" << task_used_usec << ';';
        }
        
        if (scanType & eProfileThreadsCPU)
        {
            int num_threads = threads_id.size();
            for (int i=0; i<num_threads; i++)
            {
                profile_data_stream << "thread_used_id:" << std::hex << threads_id[i] << std::dec << ';';
                profile_data_stream << "thread_used_usec:" << threads_used_usec[i] << ';';
                
                if (scanType & eProfileThreadName)
                {
                    profile_data_stream << "thread_used_name:";
                    int len = threads_name[i].size();
                    if (len)
                    {
                        const char *thread_name = threads_name[i].c_str();
                        // Make sure that thread name doesn't interfere with our delimiter.
                        profile_data_stream << RAW_HEXBASE << std::setw(2);
                        const uint8_t *ubuf8 = (const uint8_t *)(thread_name);
                        for (int j=0; j<len; j++)
                        {
                            profile_data_stream << (uint32_t)(ubuf8[j]);
                        }
                        // Reset back to DECIMAL.
                        profile_data_stream << DECIMAL;
                    }
                    profile_data_stream << ';';
                }
            }
        }
        
        if (scanType & eProfileHostMemory)
            profile_data_stream << "total:" << physical_memory << ';';
        
        if (scanType & eProfileMemory)
        {
            static vm_size_t pagesize;
            static bool calculated = false;
            if (!calculated)
            {
                calculated = true;
                pagesize = PageSize();
            }
            
            profile_data_stream << "wired:" << vm_stats.wire_count * pagesize << ';';
            profile_data_stream << "active:" << vm_stats.active_count * pagesize << ';';
            profile_data_stream << "inactive:" << vm_stats.inactive_count * pagesize << ';';
            uint64_t total_used_count = vm_stats.wire_count + vm_stats.inactive_count + vm_stats.active_count;
            profile_data_stream << "used:" << total_used_count * pagesize << ';';
            profile_data_stream << "free:" << vm_stats.free_count * pagesize << ';';
            
            profile_data_stream << "rprvt:" << rprvt << ';';
            profile_data_stream << "rsize:" << rsize << ';';
            profile_data_stream << "vprvt:" << vprvt << ';';
            profile_data_stream << "vsize:" << vsize << ';';
            
            if (scanType & eProfileMemoryDirtyPage)
                profile_data_stream << "dirty:" << dirty_size << ';';

            if (scanType & eProfileMemoryAnonymous)
            {
                profile_data_stream << "purgeable:" << purgeable << ';';
                profile_data_stream << "anonymous:" << anonymous << ';';
            }
        }
        
        profile_data_stream << "--end--;";
        
        result = profile_data_stream.str();
    }
    
    return result;
}
コード例 #24
0
/**
 * @brief synchronized create_shard/write/read/delete/delete_shard operations
 */
void
user_operations_cursor_test(uint64_t args) {
    struct replication_test_framework *test_framework =
            (struct replication_test_framework *)args;
    SDF_boolean_t op_ret = SDF_FALSE;
    struct SDF_shard_meta *shard_meta = NULL;
    SDF_replication_props_t *replication_props = NULL;
    int failed = 0;
    uint64_t seqno = 0;
    SDF_shardid_t shard_id = 2;
    vnode_t node_id = 1;
    struct timeval now;
    struct timeval when;
    /* timeval incre */
    struct timeval incre;

    void *data_read;
    size_t data_read_len;

    uint64_t          seqno_start, seqno_len, seqno_max;
    int               i;
    int               ncursors;
    it_cursor_t      *pit;
    resume_cursor_t  *prc = NULL;
    char              skey[1024];
    SDF_time_t        exptime;
    SDF_time_t        createtime;
    int               key_len;
    size_t            data_len;
    void             *pdata;
    int               resume_cursor_size = 0;
    char             *pcur;

    shard_id = __sync_add_and_fetch(&test_framework->max_shard_id, 1);
    char *key;
    char *data;

    failed = !plat_calloc_struct(&meta);
    replication_test_meta_init(meta);

    /* Assure test_framework is started?! */
    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG, "start test_framework");
    rtfw_start(test_framework);
    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG, "test_framework started\n");

    /* Start all nodes */
    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG, "start nodes");
    rtfw_start_all_nodes(test_framework);
    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG, "nodes started");

    plat_assert(!failed);
    failed = !plat_calloc_struct(&replication_props);
    plat_assert(!failed);
    rtfw_set_default_replication_props(&test_framework->config, replication_props);
    shard_meta = rtfw_init_shard_meta(&test_framework->config,
                                      1 /* first_node */,
                                      shard_id
                                      /* shard_id, in real system generated by generate_shard_ids() */,
                                      replication_props);

    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG,
                 "\n**************************************************\n"
                 "                  create shard sync                 "
                 "\n**************************************************");
    op_ret = rtfw_create_shard_sync(test_framework, 1, shard_meta);
    plat_assert(op_ret == SDF_SUCCESS);

    /* - write on node 1, key:google:1, data:Sebstian:1 */
    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG,
                 "\n**************************************************\n"
                 "                 write object sync                  "
                 "\n**************************************************");
    plat_asprintf(&key, "google:%d", 1);
    plat_asprintf(&data, "Sebstian:%d", 1);

    plat_log_msg(LOG_ID, LOG_CAT, LOG_TRACE,
                 "write key:%s, key_len:%u, data:%s, data_len:%u",
                 key, (int)(strlen(key)), data, (int)(strlen(data)));
    op_ret = rtfw_write_sync(test_framework,
                             shard_id /* shard */, 1 /* node */,
                             meta /* test_meta */,
                             key, strlen(key)+1, data, strlen(data)+1);
    plat_assert(op_ret == SDF_SUCCESS);
    plat_free(key);
    plat_free(data);

    /* - read on node 1, key:google:1 */
    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG,
                 "\n**************************************************\n"
                 "                  read object sync                  "
                 "\n**************************************************");
    replication_test_framework_read_data_free_cb_t free_cb =
        replication_test_framework_read_data_free_cb_create(PLAT_CLOSURE_SCHEDULER_ANY_OR_SYNCHRONOUS,
                                                            &rtfw_read_free,
                                                            test_framework);
    plat_asprintf(&key, "google:%d", 1);
    plat_log_msg(LOG_ID, LOG_CAT, LOG_TRACE,
                 "KEY:%s, key_len:%d", key, (int)strlen(key));

    op_ret = rtfw_read_sync(test_framework, shard_id /* shard */, node_id /* node */, key, strlen(key) + 1,
                            &data_read, &data_read_len, &free_cb);
    plat_free(key);
    plat_assert(op_ret == SDF_SUCCESS);
    plat_log_msg(LOG_ID, LOG_CAT, LOG_TRACE,
                 "read data:%s, data_len:%d", (char *)data_read, (int)data_read_len);
    plat_free(data_read);

    /* crash node 2 */
    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG,
                 "\n**************************************************\n"
                 "                  crash node 2 sync                  "
                 "\n**************************************************");
    rtfw_crash_node_sync(test_framework, 2);
    plat_log_msg(LOG_ID, LOG_CAT, LOG_TRACE,
                 "crash node:%"PRIu32" complete", 2);

    /**
     * write on node 1, key2: google:2, data2: Sebstian:2,
     * key3: google:3, data3: Sebstian:3
     */
    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG,
                 "\n**************************************************\n"
                 "                 write object sync                  "
                 "\n**************************************************");
    plat_asprintf(&key, "google:%d", 2);
    plat_asprintf(&data, "Sebstian:%d", 2);

    plat_log_msg(LOG_ID, LOG_CAT, LOG_TRACE,
                 "write key:%s, key_len:%u, data:%s, data_len:%u",
                 key, (int)(strlen(key)), data, (int)(strlen(data)));
    op_ret = rtfw_write_sync(test_framework,
                             shard_id /* shard */, 1 /* node */,
                             meta /* test_meta */,
                             key, strlen(key)+1, data, strlen(data)+1);
    plat_assert(op_ret == SDF_SUCCESS);
    plat_free(key);
    plat_free(data);

    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG,
                 "\n**************************************************\n"
                 "                 write object sync                  "
                 "\n**************************************************");
    plat_asprintf(&key, "google:%d", 3);
    plat_asprintf(&data, "Sebstian:%d", 3);

    plat_log_msg(LOG_ID, LOG_CAT, LOG_TRACE,
                 "write key:%s, key_len:%u, data:%s, data_len:%u",
                 key, (int)(strlen(key)), data, (int)(strlen(data)));
    op_ret = rtfw_write_sync(test_framework,
                             shard_id /* shard */, 1 /* node */,
                             meta /* test_meta */,
                             key, strlen(key)+1, data, strlen(data)+1);
    plat_assert(op_ret == SDF_SUCCESS);
    plat_free(key);
    plat_free(data);


    /* read on node 1, key2: google:2 */
    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG,
                 "\n**************************************************\n"
                 "                  read object sync                  "
                 "\n**************************************************");
    plat_asprintf(&key, "google:%d", 2);
    plat_log_msg(LOG_ID, LOG_CAT, LOG_TRACE,
                 "KEY:%s, key_len:%d", key, (int)strlen(key));

    op_ret = rtfw_read_sync(test_framework, shard_id /* shard */, 1 /* node */, key, strlen(key) + 1,
                            &data_read, &data_read_len, &free_cb);
    plat_free(key);
    plat_assert(op_ret == SDF_SUCCESS);
    plat_log_msg(LOG_ID, LOG_CAT, LOG_TRACE,
                 "read data:%s, data_len:%d", (char *)data_read, (int)data_read_len);
    plat_free(data_read);


    /* delete from node 1, key3: google3 */
    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG,
                 "\n***************************************************\n"
                 "                  delete object sync                 "
                 "\n***************************************************");
    plat_asprintf(&key, "google:%d", 3);
    plat_log_msg(LOG_ID, LOG_CAT, LOG_TRACE,
                 "KEY:%s, key_len:%d", key, (int)(strlen(key)));

    op_ret = rtfw_delete_sync(test_framework, shard_id /* shard */, 1 /* node */, key, strlen(key)+1);
    plat_assert(op_ret == SDF_SUCCESS);
    plat_free(key);

    /* restart node 2 */
    op_ret = rtfw_start_node(test_framework, 2);
    plat_assert(op_ret == SDF_SUCCESS);

    /* block a while */
    now = test_framework->now;
    incre.tv_sec = 10;
    incre.tv_usec = 0;
    timeradd(&now, &incre, &when);
    rtfw_block_until(test_framework, (const struct timeval)when);
    rtfw_sleep_usec(test_framework, SLEEP_US);

    /* get last seqno from node 2 */
    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG,
                 "\n************************************************************\n"
                 "                 get latest seqno from node 2                "
                 "\n************************************************************");

    op_ret = rtfw_get_last_seqno_sync(test_framework, 2, shard_id, &seqno);

    if (op_ret == SDF_SUCCESS) {
        plat_log_msg(LOG_ID, LOG_CAT, LOG_INFO, "get_last_seqno succeeded! (seqno=%"PRIu64")", seqno);
    } else {
        plat_log_msg(LOG_ID, LOG_CAT, LOG_INFO, "get_last_seqno failed!");
    }

    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG,
                 "\n************************************************************\n"
                 "                        get iteration cursors                  "
                 "\n************************************************************");

    prc = NULL;
    resume_cursor_size = 0;
    while (1) {
        replication_test_framework_read_data_free_cb_t free_cb =
            replication_test_framework_read_data_free_cb_create(PLAT_CLOSURE_SCHEDULER_ANY_OR_SYNCHRONOUS,
                                                                &rtfw_read_free,
                                                                test_framework);

        seqno_start        = 0;
        seqno_len          = 10;
        seqno_max          = UINT64_MAX - 1;
        op_ret = rtfw_get_cursors_sync(test_framework, shard_id, node_id,
                                       seqno_start, seqno_len, seqno_max,
                                       (void *) prc, resume_cursor_size,
                                       (void **) &pit, &data_len, &free_cb);

        if (op_ret != SDF_SUCCESS) {
            plat_log_msg(LOG_ID, LOG_CAT, LOG_INFO, "get_iteration_cursors failed!");
            break;
        } else {
            ncursors = pit->cursor_count;
            if (ncursors == 0) {
                break;
            }
            prc = &(pit->resume_cursor);
            resume_cursor_size = sizeof(resume_cursor_t);
            plat_assert(data_len == (sizeof(it_cursor_t) + seqno_len*pit->cursor_len));
            plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG, "get_iteration_cursors succeeded (%d cursors returned)!", ncursors);

            pcur = pit->cursors;
            for (i = 0; i < ncursors; i++) {
                replication_test_framework_read_data_free_cb_t free_cb =
                replication_test_framework_read_data_free_cb_create(PLAT_CLOSURE_SCHEDULER_ANY_OR_SYNCHRONOUS,
                                                                    &rtfw_read_free,
                                                                    test_framework);
                op_ret = rtfw_get_by_cursor_sync(test_framework, shard_id, node_id,
                                                 (void *) pcur, pit->cursor_len,
                                                 skey, 1024, &key_len,
                                                 &exptime, &createtime, &seqno,
                                                 &pdata, &data_len, &free_cb);
                pcur += pit->cursor_len;

                if (op_ret == SDF_SUCCESS) {
                    plat_log_msg(LOG_ID, LOG_CAT, LOG_TRACE,
                                 "get_by_cursor: %s, key_len:%u, data:%s, data_len:%u,"
                                 "seqno: %"PRIu64", exptime:%"PRIu32", createtime:%"PRIu32"",
                                 skey, key_len, (char *)pdata, (unsigned)data_len,
                                 seqno, exptime, createtime);
                    plat_free(pdata);
                } else {
                    plat_log_msg(LOG_ID, LOG_CAT, LOG_INFO, "get_by_cursor failed!");
                }
            }
        }
    }
    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG,
                 "\n************************************************************\n"
                 "                  Test framework shutdown                       "
                 "\n************************************************************");
    rtfw_shutdown_sync(test_framework);

    plat_log_msg(LOG_ID, LOG_CAT, LOG_DBG,
                 "\n************************************************************\n"
                 "                  Test framework sync summary                 "
                 "\n************************************************************");
    plat_free(meta);
    plat_free(replication_props);
    plat_free(shard_meta);

    /* Terminate scheduler if idle_thread exit */
    while (test_framework->timer_dispatcher) {
        fthYield(-1);
    }
    plat_free(test_framework);

    fthKill(1);
}
コード例 #25
0
ファイル: timer.c プロジェクト: larshb/TTK4145
void timer_set(struct timeval* timeout, int msec) {
	_temp_time.tv_sec = msec / 1000;
	_temp_time.tv_usec = (msec % 1000) * 1000;
	gettimeofday(&_current_time, 0);
	timeradd(&_current_time, &_temp_time, timeout);
}
コード例 #26
0
/* If the VM is running as a single instance and there is a pre-existing
 * instance then look for a pre-existing instance and if found send it a
 * drop event of the argument and if successful exit.  Otherwise return
 * and allow the normal start-up sequence to continue.
 */
static int
dndLaunchFile(char *filename)
{
	long data[5];
	char abspath[MAXPATHLEN+1];
	struct timeval start, now, timeout;
	time_t tnow;
	int pid = getpid();
	Window target;

	tnow = time(0);
	printf("dndLaunchFile(%s,%d) \"%s\" %s", filename, pid, defaultWindowLabel, ctime(&tnow));
	target = findWindowWithLabel(DefaultRootWindow(stDisplay), defaultWindowLabel);

	if (!target) {
		tnow = time(0);
		printf("dndLaunchFile(%s,%d) %s\tFAILED TO FIND WINDOW:\"%s\"\n", filename, pid, ctime(&tnow), defaultWindowLabel);
		return 0;
	}

	if (*filename == '/')
		strcpy(abspath,filename);
	else {
		/* For consistency with drops files should be relative to the image.
		 * For sanity creating streams drops should be absolute paths (i.e.
		 * primDropRequestFileHandle: doesn't know what the image path is and
		 * so interprets things relative to pwd, so give it an absolute path).
		 * So by default make the full path by prepending the image.
		 */
#if !defined(DROP_FILENAMES_RELATIVE_TO_PWD)
# define DROP_FILENAMES_RELATIVE_TO_PWD 0
#endif
#if DROP_FILENAMES_RELATIVE_TO_PWD
		getcwd(abspath,sizeof(abspath));
		abspath[strlen(abspath)] = '/';
		strcat(abspath,filename);
#else
		strcpy(abspath,imageName);
		strcpy(strrchr(abspath,'/')+1,filename);
#endif
	}

	/* Only drop if the file exists. */
    if (access(abspath, F_OK|R_OK)) {
		tnow = time(0);
		printf("dndLaunchFile(%s,%d) %s\tFAILED TO VALIDATE:\"%s\"\n", filename, pid, ctime(&tnow), abspath);
		return 0;
	}

	tnow = time(0);
	printf("dndLaunchFile(%s,%d) %s\tvalidated:\"%s\"\n", filename, pid, ctime(&tnow), abspath);

	/* Include the null in the filename so that we're immune to XGetWindowProp'
	 * answering the size in 32-bit units.
	 */
	XChangeProperty(stDisplay, stParent,
			XdndSqueakLaunchDrop, XA_ATOM, 8, PropModeReplace,
			(unsigned char *)abspath, strlen(abspath) + 1);

	memset(data, 0, sizeof(data));
	data[0] = stParent; /* => xdndDrop_sourceWindow */
	sendClientMessage(data, stParent, target, XdndSqueakLaunchDrop);

	/* How can there be 10 odd get event functions and yet none provide
	 * peek with timeout functionality?  X is sad.
	 */
	timeout.tv_sec = launchDropTimeoutMsecs / 1000;
	timeout.tv_usec = (launchDropTimeoutMsecs % 1000) * 1000;
	gettimeofday(&start, 0);
	timeradd(&start, &timeout, &timeout);

	do {
		XEvent evt;
		/* Don't spin hard; the dnd recipient needs cycles to receive and ack. */
		yieldCyclesToRecipient();
		if (XCheckIfEvent(stDisplay, &evt, isDropAck, 0)) {
			tnow = time(0);
			printf("dndLaunchFile(%s,%d) %s\tgot drop ack for:\"%s\"\n", filename, pid, ctime(&tnow), abspath);
			return 1;
		}
		gettimeofday(&now, 0);
	}
	while (timercmp(&now, &timeout, <));
	tnow = time(0);
	printf("dndLaunchFile(%s,%d) %s\t%ld msec DROP TIMEOUT FOR:\"%s\"\n", filename, pid, ctime(&tnow), launchDropTimeoutMsecs, abspath);
	return 0;
}
コード例 #27
0
ファイル: c_sh.c プロジェクト: Open343/bitrig
/*
 * time pipeline (really a statement, not a built-in command)
 */
int
timex(struct op *t, int f, volatile int *xerrok)
{
#define TF_NOARGS	BIT(0)
#define TF_NOREAL	BIT(1)		/* don't report real time */
#define TF_POSIX	BIT(2)		/* report in posix format */
	int rv = 0;
	struct rusage ru0, ru1, cru0, cru1;
	struct timeval usrtime, systime, tv0, tv1;
	int tf = 0;
	extern struct timeval j_usrtime, j_systime; /* computed by j_wait */

	gettimeofday(&tv0, NULL);
	getrusage(RUSAGE_SELF, &ru0);
	getrusage(RUSAGE_CHILDREN, &cru0);
	if (t->left) {
		/*
		 * Two ways of getting cpu usage of a command: just use t0
		 * and t1 (which will get cpu usage from other jobs that
		 * finish while we are executing t->left), or get the
		 * cpu usage of t->left. at&t ksh does the former, while
		 * pdksh tries to do the later (the j_usrtime hack doesn't
		 * really work as it only counts the last job).
		 */
		timerclear(&j_usrtime);
		timerclear(&j_systime);
		rv = execute(t->left, f | XTIME, xerrok);
		if (t->left->type == TCOM)
			tf |= t->left->str[0];
		gettimeofday(&tv1, NULL);
		getrusage(RUSAGE_SELF, &ru1);
		getrusage(RUSAGE_CHILDREN, &cru1);
	} else
		tf = TF_NOARGS;

	if (tf & TF_NOARGS) { /* ksh93 - report shell times (shell+kids) */
		tf |= TF_NOREAL;
		timeradd(&ru0.ru_utime, &cru0.ru_utime, &usrtime);
		timeradd(&ru0.ru_stime, &cru0.ru_stime, &systime);
	} else {
		timersub(&ru1.ru_utime, &ru0.ru_utime, &usrtime);
		timeradd(&usrtime, &j_usrtime, &usrtime);
		timersub(&ru1.ru_stime, &ru0.ru_stime, &systime);
		timeradd(&systime, &j_systime, &systime);
	}

	if (!(tf & TF_NOREAL)) {
		timersub(&tv1, &tv0, &tv1);
		if (tf & TF_POSIX)
			p_time(shl_out, 1, &tv1, 5, "real ", "\n");
		else
			p_time(shl_out, 0, &tv1, 5, NULL, " real ");
	}
	if (tf & TF_POSIX)
		p_time(shl_out, 1, &usrtime, 5, "user ", "\n");
	else
		p_time(shl_out, 0, &usrtime, 5, NULL, " user ");
	if (tf & TF_POSIX)
		p_time(shl_out, 1, &systime, 5, "sys  ", "\n");
	else
		p_time(shl_out, 0, &systime, 5, NULL, " system\n");
	shf_flush(shl_out);

	return rv;
}