예제 #1
0
void run( int tile,   float * A,  float * B,  float * C,  int N )
{
  struct timeval tv_start, tv_stop;

  gettimeofday(&tv_start, NULL);

#define CALL( fun ) fun( A, B, C, N );

  switch ( tile ) {
  case    0: CALL(MulMatrixCPU); break;
  case    8: CALL(GPUMatrixMul_T8); break;
  case   16: CALL(GPUMatrixMul_T16); break;
  }

#undef CALL

  gettimeofday(&tv_stop, NULL);

  if ( tile )
    {
      printf("GPU(T=%d) time: %f\n",
             tile,
             timevaldiff(tv_start,tv_stop));
    }
  else // CPU
    {
      printf("CPU time: %f\n",
             timevaldiff(tv_start,tv_stop));
    }

}
예제 #2
0
파일: ledmax.c 프로젝트: pakohan/syso-ng
int  main()
{
    pthread_t blinker;
    struct sigaction new_action;
    int device, i;
	struct timeval start, end;

	device = open(DEVICE, O_RDWR);
	if (device < 0)
	{
		fprintf(stderr, "could not open " DEVICE "\n");
	}	
	
	gettimeofday(&start, NULL);
	for (i = 0; i < NUMBER; i++)
	{
		turn_on(device);
		turn_off(device);
    }
    gettimeofday(&end, NULL);
    	
	printf("%lf usec\n", timevaldiff(&start, &end) / NUMBER);

	close(device);
      
    return 0;
}
예제 #3
0
bool compare_times(struct timeval *first, struct timeval *second) {
	long msec;

	msec = timevaldiff(first, second);
	if (msec <= TIME_THRESHOLD)
		return true;
	return false;
}
예제 #4
0
파일: process.c 프로젝트: NeoRaider/radvd
static void process_rs(struct Interface *iface, unsigned char *msg, int len, struct sockaddr_in6 *addr)
{
	double delay;
	double next;
	struct timeval tv;
	uint8_t *opt_str;

	/* validation */
	len -= sizeof(struct nd_router_solicit);

	opt_str = (uint8_t *) (msg + sizeof(struct nd_router_solicit));

	while (len > 0) {
		int optlen;

		if (len < 2) {
			flog(LOG_WARNING, "trailing garbage in RS");
			return;
		}

		optlen = (opt_str[1] << 3);

		if (optlen == 0) {
			flog(LOG_WARNING, "zero length option in RS");
			return;
		} else if (optlen > len) {
			flog(LOG_WARNING, "option length greater than total length in RS");
			return;
		}

		if (*opt_str == ND_OPT_SOURCE_LINKADDR && IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr)) {
			flog(LOG_WARNING, "received icmpv6 RS packet with unspecified source address and there is a lladdr option");
			return;
		}

		len -= optlen;
		opt_str += optlen;
	}

	gettimeofday(&tv, NULL);

	delay = MAX_RA_DELAY_TIME * rand() / (RAND_MAX + 1.0);

	if (iface->UnicastOnly) {
		send_ra_forall(iface, &addr->sin6_addr);
	} else if (timevaldiff(&tv, &iface->last_multicast) / 1000.0 < iface->MinDelayBetweenRAs) {
		/* last RA was sent only a few moments ago, don't send another immediately. */
		next =
		    iface->MinDelayBetweenRAs - (tv.tv_sec + tv.tv_usec / 1000000.0) + (iface->last_multicast.tv_sec + iface->last_multicast.tv_usec / 1000000.0) + delay / 1000.0;
		iface->next_multicast = next_timeval(next);
	} else {
		/* no RA sent in a while, send a multicast reply */
		send_ra_forall(iface, NULL);
		next = rand_between(iface->MinRtrAdvInterval, iface->MaxRtrAdvInterval);
		iface->next_multicast = next_timeval(next);
	}
}
예제 #5
0
int main() {

  struct timeval start, finish;
  long msec;

  gettimeofday(&start, NULL);
  sleep(1);
  gettimeofday(&finish, NULL);
  msec = timevaldiff(&start, &finish);
  printf("Elapsed time for sleep(1) is: %d milliseconds.\n", msec);
  return 0;
}
예제 #6
0
int main()
{
    struct timeval s,f;

    //1
    std::tr1::unordered_map< int, int> m;
    gettimeofday( &s, NULL );
    int n = 1000000,tot;
    for(int i=0;i<n;i++){
        m[i]=i*10;
    }
    gettimeofday( &f, NULL );
    std::cerr << "unom-create:"<<timevaldiff( &s, &f) << std::endl;
    // 1-1
    gettimeofday( &s, NULL );
    tot=0;
    for(int i=0;i<n;i++){
        tot += m[i];
    }
    gettimeofday( &f, NULL );
    std::cerr << "unom-read:"<<timevaldiff( &s, &f) << std::endl;
    // 1-2
    gettimeofday( &s, NULL );
    for(int i=0;i<n;i++){
        m.erase(i);
    }
    gettimeofday( &f, NULL );
    std::cerr << "unom-erase:"<<timevaldiff( &s, &f) << " sz:" << m.size() << std::endl;
    //2
    gettimeofday( &s, NULL );
    std::map<int,int>mm;
    for(int i=0;i<n;i++){
        mm[i]=i*10;
    }
    gettimeofday( &f, NULL );
    std::cerr << "stdm-create:"<<timevaldiff( &s, &f) << std::endl;
    //2-1
    gettimeofday( &s, NULL );
    tot=0;
    for(int i=0;i<n;i++){
        tot += mm[i];
    }
    gettimeofday( &f, NULL );        
    std::cerr << "stdm-read:"<<timevaldiff( &s, &f) << std::endl;
    // 1-2
    gettimeofday( &s, NULL );
    for(int i=0;i<n;i++){
        mm.erase(i);
    }
    gettimeofday( &f, NULL );
    std::cerr << "stdm-erase:"<<timevaldiff( &s, &f) << " sz:" << mm.size() << std::endl;    
}
예제 #7
0
static void  network_udp_start(void)
{   
    bzero(&receive_addr, sizeof(struct sockaddr_in));
    receive_addr.sin_family = AF_INET;
    receive_addr.sin_addr.s_addr = INADDR_ANY;
    receive_addr.sin_port = htons(3338);

    bzero(&broadcast_addr, sizeof(struct sockaddr_in));
    broadcast_addr.sin_family = AF_INET;
    broadcast_addr.sin_addr.s_addr = INADDR_BROADCAST;
    broadcast_addr.sin_port = htons(sendPort);

    if (-1 == (server_sock = socket(AF_INET, SOCK_DGRAM, 0))) {
        printf("E > socket error\n\r");
        return;
    }

    int broadcast = 1;
    if (setsockopt(server_sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) < 0) {
        printf("E > socket options error\n\r");
        return;
    }

    struct timeval tv;
    tv.tv_sec = 3;
    tv.tv_usec = 0;
    if (setsockopt(server_sock, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) {
        printf("E > socket options error\n\r");
        return;
    }

    if (-1 == bind(server_sock, (struct sockaddr *)(&receive_addr), sizeof(struct sockaddr))) {
        printf("E > bind fail\n\r");
        return;
    }

    const int recv_len = 256;
    char *recv_buf = (char *)malloc(recv_len);
    char recv_addr_b[sizeof(struct sockaddr_in)];
    socklen_t addr_len = sizeof(struct sockaddr_in);
    struct timeval system_time, compare_time;

    const char packet_type_detect[] = "SimpleUDP_detect\nOWN\n";

    // We have been started right now: Send broacast message that we are on
    network_data_received(0, (char*)packet_type_detect, sizeof(packet_type_detect)-1);

    if (DEBUG) printf("D > Wait on port: %d\n\r", ntohs(receive_addr.sin_port));
    int recbytes;
    const int flags = 0;

    for (;;) {
        gettimeofday(&system_time, 0);
        errno = 0;
        while ((recbytes = recvfrom(server_sock, recv_buf, recv_len, flags, (struct sockaddr *)recv_addr_b, &addr_len)) > 0) {
            recv_buf[recbytes] = 0;
	    const int t = sizeof(struct sockaddr_in);
	    if (addr_len != t) {
		printf("E > read peer address failed! %d %d\n\r", (int)addr_len, t);
		continue;
	    }
	    struct sockaddr_in copy;
	    memcpy(&copy, &recv_addr_b, sizeof(struct sockaddr_in));
            network_data_received(&copy, recv_buf, recbytes);
        }

        if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
            gettimeofday(&compare_time, 0);
            if (timevaldiff(&system_time, &compare_time) > 5000) {
                if (DEBUG) printf("D > Resume from suspend\n\r");
                // We have been suspended (system time differs more than 5 seconds): Send broacast message that we are on again
                network_data_received(0, (char*)packet_type_detect, sizeof(packet_type_detect)-1);
            }
            continue;
        }

        if (recbytes < 0) {
            perror("E > read data fail: ");
            break;
        }
    }
    free(recv_buf);
    close(server_sock);
    server_sock = -1;
}
예제 #8
0
파일: video_split.cpp 프로젝트: szqh97/test
// TODO wirite shot boundary info to files
// 1. deal m_curr_frame_ts
// 2. timediff of two video file is large
int video_split_processor::video_split(FILE *fp, off_t pos, video_file_info *p_info, \
        live_timeval &task_begin_time, live_timeval &task_end_time) 
{

    off_t len = p_info->width * p_info->height * 2;
    size_t mapped_size = 0;
    unsigned char *mapped_buffer = map_file(fp, len * (int)p_info->frame_count, \
            mapped_size);   int fts = 1000 / p_info->frame_rate;
    
    write_yuv_file(mapped_buffer, mapped_size, p_info);
    if (timevaldiff(m_prev_end_time, p_info->begin_time) < fts)
    {

        unsigned char frame_buf[len];
        memset(frame_buf, 0, len);
        int y_size = p_info->width * p_info->height;
        unsigned char *ybuffer = (unsigned char*)malloc(y_size);
        memset(ybuffer, 0, y_size);
        for (int i = 0; i < p_info->frame_count; ++i)
        {
            memcpy(frame_buf, mapped_buffer + i * len, len);
            //posix_memalign((void **)ybuffer, 32, y_size);
            get_Y(p_info->color_type, frame_buf, ybuffer, y_size); 

            // first time detect invoke restart after init
            if (!mb_initialized)
            {
                // 1. init video_split_processor
                // 2. create a new yuv file
                memcpy(&m_shot_begin_time, &p_info->begin_time, sizeof(live_timeval));
                mp_sd = new shot_detector((uint32_t)p_info->width, \
                        (uint32_t)p_info->height, 4, 4, m_cfg);
                memset(m_tmp_video, 0, FILE_NAME_LEN);
                
                // shot info file
                char shot_file[FILE_NAME_LEN];
                memset(shot_file, 0, FILE_NAME_LEN);
                sprintf(shot_file, "%s/%lld%03lld.%lld%03lld.xml", m_shot_path, \
                        task_begin_time.tv_sec, task_begin_time.tv_usec /1000,
                        task_end_time.tv_sec, task_end_time.tv_usec / 1000);
                m_fshot = fopen(shot_file, "w");
                if (!m_fshot)
                {
                    // open failed
                    cout << " open " << shot_file << "failed" << endl;
                    return -1;
                }
                string s = "</shots>\n";
                cout << "write:"<< shot_file << endl;
                size_t l= fwrite(s.c_str(), s.size(), 1, m_fshot);
                cout << "tettttttt " << l << endl;



                mp_sd->restart(ybuffer, fts);
                //size_t s = fwrite(frame_buf, len, 1, m_fp);
                m_curr_frame_ts = fts;

                memcpy(&m_shot_begin_time, &p_info->begin_time, sizeof(live_timeval));
                mb_initialized = true;
#if DEBUG
#endif 
            }
            else
            {
                // TODO
                // 1. detect shot
                // 2. write frame to yuv file
                // 3. frame offset

                m_curr_frame_ts +=  fts;

                m_curr_shot = mp_sd->detect(ybuffer, m_curr_frame_ts);
                //fwrite(frame_buf, len, 1, m_fp);
                if (m_curr_shot.start > 0 and m_curr_shot.end > 0)
                {
                    write_shot_info();
                    cout << "shot info: " << m_curr_shot.start << ", " << m_curr_shot.end  << ", " <<  m_curr_shot.start_frame_type << "," << m_curr_shot.end_frame_type << endl;

                    memcpy(&m_shot_end_time, &p_info->begin_time, sizeof(live_timeval));

                    // current shot end time
                    timeval tv;
                    unsigned int t_seconds = (p_info->begin_time.tv_usec +  i * fts*1000) / 1000000;
                    tv.tv_usec = (p_info->begin_time.tv_usec + i * fts*1000) % 1000000;
                    tv.tv_sec = p_info->begin_time.tv_sec + t_seconds;
                    m_shot_end_time.tv_sec = tv.tv_sec;
                    m_shot_end_time.tv_usec = tv.tv_usec;

                    // next shot begin time
                    tv.tv_usec = (tv.tv_usec + fts * 1000) % 1000000;
                    t_seconds = (tv.tv_usec + fts * 1000) / 1000000;
                    tv.tv_sec = tv.tv_sec + t_seconds;

                    m_shot_begin_time.tv_sec = tv.tv_sec;
                    m_shot_begin_time.tv_usec = tv.tv_usec;
                    memset(m_tmp_video, 0, FILE_NAME_LEN);
                    sprintf(m_tmp_video, "%s/tmp_%lld_%03lld.yuv", m_video_path, \
                            p_info->begin_time.tv_sec, p_info->begin_time.tv_usec / 1000 );
                    //m_fp = fopen(m_tmp_video, "w");

                }
            }
        }
        free(ybuffer);
    }
    else
    {
        // lost frames
    }
    memcpy(&m_prev_end_time, &(p_info->end_time), sizeof(live_timeval));
    // if the last video_file to deal
    memcpy(&m_shot_end_time, &(p_info->end_time), sizeof(live_timeval));

    return 0;

}
예제 #9
0
int main(int argc, char **argv)
{
    char *outfilename = (char*)calloc(MAXNAMESIZE, sizeof(char));
    char *infilename = (char*)calloc(MAXNAMESIZE, sizeof(char));
    long kmin, kmax, n, chunksize, clustersize;
    int dim;
    int threads=1;			/*Last input should be number of threads*/
    timer start, end;

    fprintf(stderr,"Streamcluster Kernel\n");
    fflush(NULL);

    if (argc<10) {
        fprintf(stderr,"usage: %s k1 k2 d n chunksize clustersize infile outfile threads\n",
            argv[0]);
        fprintf(stderr,"  k1:          Min. number of centers allowed\n");
        fprintf(stderr,"  k2:          Max. number of centers allowed\n");
        fprintf(stderr,"  d:           Dimension of each data point\n");
        fprintf(stderr,"  n:           Number of data points\n");
        fprintf(stderr,"  chunksize:   Number of data points to handle per step\n");
        fprintf(stderr,"  clustersize: Maximum number of intermediate centers\n");
        fprintf(stderr,"  infile:      Input file (if n<=0)\n");
        fprintf(stderr,"  outfile:     Output file\n");
        fprintf(stderr,"  threads:     Number of threads\n");
        fprintf(stderr,"\n");
        fprintf(stderr, "if n > 0, points will be randomly generated instead of reading from infile.\n");
        exit(1);
    }

    kmin = atoi(argv[1]);
    kmax = atoi(argv[2]);
    dim = atoi(argv[3]);
    n = atoi(argv[4]);
    chunksize = atoi(argv[5]);
    clustersize = atoi(argv[6]);
    strcpy(infilename, argv[7]);
    strcpy(outfilename, argv[8]);
    threads=atoi(argv[9]);
    srand48(SEED);
	SimStream stream;
	if(n > 0) {
		stream.n = n;
	} else {
		fprintf(stderr, "Currently no input files supported!\n");
		exit(EXIT_FAILURE);
	}

    TIME(start);

    /**************************************SET THREADS NUMBER**************************************************/
    omp_set_num_threads(threads);
 
    streamCluster(&stream, kmin, kmax, dim, chunksize, clustersize, outfilename );

    TIME(end);

    double ctime_msec = (double)timevaldiff(&start, &end);

    printf("Compute time: %ds (%d msec)\n", (int)(ctime_msec/1000), (int)ctime_msec);

    free(outfilename);
    free(infilename);

    return 0;
}
예제 #10
0
파일: scan.c 프로젝트: Rventric/opendias
SANE_Byte *collectData( char *uuid, SANE_Handle *openDeviceHandle, size_t totbytes, int bpl, char *header ) {

  SANE_Status status;
  SANE_Int received_length_from_sane = 0;
  SANE_Byte *raw_image;
  SANE_Byte *raw_image_current_pos;
  int progress = 0;
  int feedback = 5;
  int bufferSize = 32768;
  size_t readSoFar = 0;
  size_t headerLength = strlen(header);
  size_t stillToRead = totbytes;
  struct timeval start, end;

  // Initialise the blank image;
  raw_image = calloc( totbytes + headerLength + 1, sizeof(unsigned char) );
  if( raw_image == NULL ) {
    o_log(ERROR, "Out of memory, when assiging the new image storage.");
    return NULL;
  }
  strcpy((char *)raw_image, header);
  raw_image_current_pos = raw_image + headerLength;


  //status = sane_set_io_mode (openDeviceHandle, SANE_TRUE);
  //o_log(DEBUGM, "setting non-blocking mode was %s", sane_strstatus( status ) );
  o_log(DEBUGM, "scan_read - start");


  gettimeofday(&start, NULL);
  do {

    // Set status as 'scanning'
    if ( 0 > feedback ) {
      updateScanProgress(uuid, SCAN_SCANNING, progress);
      feedback = 5;
    }
    feedback--;


    //
    // Read buffer from sane (the scanner)
    //status = sane_read (openDeviceHandle, raw_image_current_pos, stillToRead, &received_length_from_sane);
    status = sane_read (openDeviceHandle, raw_image_current_pos, bufferSize, &received_length_from_sane);
    o_log(DEBUGM, "At %d%, requested %d bytes, got %d, with status %d)", progress, stillToRead, received_length_from_sane, status);


    //
    // Write the read 'buffer' onto 'raw_image'
    if( received_length_from_sane > 0 ) {

      readSoFar += received_length_from_sane;
      stillToRead -= received_length_from_sane;
      raw_image_current_pos += received_length_from_sane;
      if ( received_length_from_sane == bufferSize ) {
        bufferSize += 32768;
      }
      if ( bufferSize > stillToRead ) {
        bufferSize = stillToRead;
      }

      // Update the progress info
      progress = (int)((readSoFar*100) / totbytes);
      if(progress > 100)
        progress = 100;

      if ( stillToRead == 0 ) {
        o_log(ERROR, "No more bytes to read" );
        break;
      }

    } // get some data from sane


    if (status != SANE_STATUS_GOOD) {
      if (status == SANE_STATUS_EOF) {
        o_log(ERROR, "sane told us were at the end" );
        break;
      }
      else {
        o_log(ERROR, "something wrong while scanning: %s", sane_strstatus(status) );
        break;
      }
    }

  } while (1);

  gettimeofday(&end, NULL);
  o_log(DEBUGM, "scan_read - end." );
  o_log( INFORMATION, "Read %d of an expected %d bytes, in %lu ms", readSoFar, totbytes, timevaldiff(&start, &end) );

  return raw_image;
}