Exemplo n.º 1
0
void c_trial() {
  int** A;
  int i;
  double starttime, endtime;
  int worked = 1;
  A = (int**)malloc(size*sizeof(int*));
  if (A == NULL) {
    printf("malloc failed\n");
  }
  assign(A, size);
  starttime = now_time();
  for (i=0; i<ntrials; i++) {
    if (touch(A, size) != 999000000) {
      worked = 0;
    }
  }
  endtime = now_time();
  if (!worked) {
    printf("Error, C miscalculation!\n");
  }
  printf("C    accessed [%d][%d] elements %d times in %f seconds\n", size, size,
         ntrials, (endtime-starttime)/(1000*1000));
  free(A);
}
Exemplo n.º 2
0
void outprint_hex(uint8_t * descrip, char *instr, uint16_t inlen )
{
	uint32_t  i=0;
	uint8_t *THstr = NULL;
	THstr=(uint8_t*)malloc(sizeof(uint8_t)*inlen+1);
	memcpy(THstr,instr,inlen);
	printf("\n%s: (%d)>>%x\n",descrip,inlen,now_time());
	for( i=0;i<inlen;i++)
	{
	printf("%02X ",THstr[i]);
	if((i+1)%16==0)
	{
	printf("\n");
	}
	}

	printf("\n");
	free(THstr);
}
Exemplo n.º 3
0
void start_server(int port, Writer *writer)
{
    boost::asio::io_service io_service;

    boost::asio::ip::udp::socket socket(io_service, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 0));
    socket.set_option(boost::asio::socket_base::broadcast(true));

    boost::asio::ip::udp::endpoint broadcast_endpoint(boost::asio::ip::address_v4::broadcast(), port);

    const std::string MAC_ADDRES = get_mac_addres();
    const std::string HOST_NAME = boost::asio::ip::host_name();

    while(true)
    {
        std::string buffer = merge(MAC_ADDRES, HOST_NAME, now_time());

        socket.send_to(boost::asio::buffer(buffer), broadcast_endpoint);

        *writer << "SERVER sent :" << buffer << "\n";

        std::this_thread::sleep_for(std::chrono::seconds(5));
    }
}
Exemplo n.º 4
0
Arquivo: ra.c Projeto: AbheekG/chapel
int main(int argc, char* argv[]) {
  unsigned long long i, locN_U, locStart, locEnd;
  rndtype r, recvBuf[2];
  MPI_Request recvReq;
  int base, iter, errorCnt = 0, totalErrors;
  double starttime, finishtime, exectime;

  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

  parseArgs(argc, argv);

  if (rank == 0) {
    printf("Number of nodes = %d\n", nprocs); fflush(stdout);
    printf("Problem size = %lld (2**%d)\n", m, n); fflush(stdout);
    printf("Number of updates = %lld (2**%d)\n", N_U, N_U_log2); fflush(stdout);
  }
  randInit();

  A = malloc(sizeof(rndtype)*m/nprocs);
  if (A == NULL) {
    printf("malloc failed for main array.\n"); fflush(stdout);
    MPI_Abort(MPI_COMM_WORLD, 1);
  }
  
  base = rank * (m / nprocs);
  for (i=0; i < m / nprocs; i++) {
    A[i] = i + base;
  }

  MPI_Barrier(MPI_COMM_WORLD);
  // first iteration does the benchmark, second reverses it for validation.
  for (iter=0; iter < 2; iter++) {
    doneCount = 0;
    doneWithRecvs = 0;

    if (iter == 0) {
      starttime = now_time();
    }

    MPI_Irecv(recvBuf, 2*sizeof(rndtype), MPI_BYTE, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, &recvReq);

    locN_U = N_U / nprocs;
    locStart = locN_U * rank;
    locEnd = locStart+locN_U;
    r = getNthRandom(locStart);
    for (i=locStart; i < locEnd; i++) {
      int loc = idxToLocale(r & idxMask);
      if (loc < 0 || loc > nprocs-1) {
        printf("error: r=%llu, r&mask=%llu, loc=%d\n", r, r&idxMask, loc);
      }
      if (loc == rank) {
        A[ind2localIdx(r & idxMask)] ^= r;
      } else {
        dosend(loc, r, 0, &recvReq, recvBuf);
      }
      getNextRandom(&r);
    }
    for (i = 0; i < nprocs; i++) {
      dosend(i, 0, 1, &recvReq, recvBuf);
    }
    while (!doneWithRecvs) {
      tryRecv(&recvReq, recvBuf);
    }

    MPI_Barrier(MPI_COMM_WORLD);
    if (iter == 0) {
      finishtime = now_time();
      exectime = (finishtime - starttime) / 1.0e+6;
    }
  }

  for (i=0; i < m/nprocs; i++) {
    if (A[i] != i + base) {
      errorCnt++;
    }
  }

  MPI_Reduce(&errorCnt, &totalErrors, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);

  if (rank == 0) {
    printf("Number of errors: %d\n", totalErrors); fflush(stdout);
    if (totalErrors <= errorTolerance * N_U) {
      printf("Validation: SUCCESS\n"); fflush(stdout);
    } else {
      printf("Validation: FAILURE\n"); fflush(stdout);
    }
    printf("Execution time = %lf\n", exectime); fflush(stdout);
    printf("Performance (GUPS) = %lf\n", (N_U / exectime) * 1.0e-9); fflush(stdout);
  }

  MPI_Finalize();

  return 0;
}
Exemplo n.º 5
0
void write_cb(struct ev_loop *loop, struct ev_io *w, int revents)
{ 
    if (EV_ERROR & revents)
    {
        perror(" w got invalid event");
        return;
    }
	client *session = (client*) (((char*)w) - offsetof(client, ev_write));
        if (session->fd == 0 )
        { 
            return;
        }
        switch (session->state) {
        
            case CREATED: {
                const char *head;
		char meta[255];
		int meta_fd;
		ssize_t size;
		char meta_data[2048];

		sprintf(meta, "%s/%s/flv", session->g_path, session->name);
		if (access(meta, R_OK) == 0) {
		    head = flv_head;
                    session->media= FLV;
                }
		else {
		    sprintf(meta, "%s/%s/ts", session->g_path, session->name);
		    head = ts_head;
                    session->media= TS ;
		}
                //printf("%s\n", meta);
		meta_fd = open(meta, O_RDONLY);
                if (meta_fd) {
		    size = read(meta_fd, meta_data, sizeof(meta_data));
		    close(meta_fd);
                } else
                {
                    head = error_head;
                }
		if (revents & EV_WRITE){
		    //write(w->fd, head, strlen(head));
                    ssize_t len = safe_send(w->fd, (void*)head, strlen(head));
                    if (len < 0)  {
                       if (errno == EAGAIN)
                           break;
                       else {
			       ev_io_stop(work_loop, &session->ev_write);
			       ev_io_stop(work_loop, &session->ev_read);
                           if (w->fd) {
                               close(w->fd);
                               session->fd = 0;
                           }
                           break;
                       }
                    }
                    //channel not found
                    if (head == error_head) {
			ev_io_stop(work_loop, &session->ev_write);
			ev_io_stop(work_loop, &session->ev_read);
                        if (w->fd) {
                            close(w->fd);
                            session->fd = 0;
                        }
                        break;
                    }
                    len = safe_send(w->fd, meta_data, size);
                    if (len < 0)  {
                       if (errno == EAGAIN)
                           break;
                       else {
			       ev_io_stop(work_loop, &session->ev_write);
			       ev_io_stop(work_loop, &session->ev_read);
                               if (w->fd) {
                                   close(w->fd);
                                   session->fd = 0;
                               }
                           break;
                       }
                    }
		}
                session->state = SEEK;
                break;
            }
            case SEEK: {
                char timestr [64];
                char indexstr [64];
                char fullpath[255];
                int sec;
                if (session->media == TS) {
                    struct tm start;
                    localtime_r(&session->start_time, &start);
                    sec = start.tm_sec % 10;
                    start.tm_sec = start.tm_sec - sec;
                    session->start_time -= sec;
                    strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S.ts", &start);
                    strftime(indexstr, sizeof(timestr), "%Y%m%d%H%M%S.index", &start);
                }else {
                    struct tm start;
                    localtime_r(&session->start_time, &start);
                    sec = start.tm_sec;
                    strftime(timestr, sizeof(timestr), "%Y%m%d%H%M.flv", &start);
                    strftime(indexstr, sizeof(timestr), "%Y%m%d%H%M.index", &start);
                }
                //printf("seek %s\n", timestr);
                off_t pos = 0;
                int fd;
                sprintf(fullpath, "%s/%s/%s", session->g_path, session->name, indexstr);
                fd = open(fullpath, O_RDONLY);
                if (fd > 0) {
                    lseek(fd, 4 * sec, SEEK_SET);
                    read(fd, &pos, sizeof(pos));
                    close(fd);
                }
                sprintf(fullpath, "%s/%s/%s", session->g_path, session->name, timestr);
                //printf("%s\n", fullpath);
                session->file_fd = open(fullpath, O_RDONLY);
                if (session->file_fd < 0) {
                    printf("no channel %s\n", fullpath);
		    ev_io_stop(work_loop, &session->ev_write);
		    ev_io_stop(work_loop, &session->ev_read);
		    close(w->fd);
                    session->fd = 0;
                    break;
                }
                if (pos > 0)
                    lseek(session->file_fd, pos, SEEK_SET);
                session->state = RUNNING;
                break;
            }
            case RUNNING: {
                int interval = 0;
                if (session->media == TS) { //ts
                    unsigned char buffer[TSPACKET_SIZE * 7];
                    unsigned char *p ;
                    uint32_t has_pcr = 0;
                    uint64_t pcr = 0;
                    int64_t diff_time = 0;
                    int64_t diff_pcr = 0;
                    ssize_t len = read(session->file_fd, buffer, sizeof(buffer));
                    if (len <= 0) {
                        ///////////////////////////
                        close(session->file_fd);
                        struct tm start;
                        char timestr[64];
                        char fullpath[255];
                        session->start_time += 10;
                        localtime_r(&session->start_time, &start);
                        strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S.ts", &start);
                        sprintf(fullpath, "%s/%s/%s", session->g_path, session->name, timestr);

                        session->file_fd = open(fullpath, O_RDONLY);
                        if (session->file_fd < 0)
                        {
                            if (session->cfg_start_time != 0) {
                                 session->start_time = session->cfg_start_time - 10;
                            }
                            else
                                 session->start_time += 10;
                        }
                        session->inited_time = 0;
                        break;
                    }
                    int i;
                    for (i = 0; i < 7; i++)
                    {
                       if ((i * TSPACKET_SIZE) >= len)
                           break;
                       p = buffer + i * TSPACKET_SIZE;
                       if (TSPACKET_HAS_ADAPTATION(p))
                       {
                           if (TSPACKET_GET_ADAPTATION_LEN(p) > 0)
                           {
                               if (TSPACKET_IS_PCRFLAG_SET(p)) {
                                  has_pcr  = 1;
                                   pcr = (TSPACKET_GET_PCRBASE(p)) / 45 ;
                                   if (session->inited_time > 0) {
                                       diff_pcr = pcr - session->last_pcr;
                                       session->last_pcr = pcr;
                                   } else {
                                       session->play_duration = 0;
                                       session->send_duration = 0;
                                       session->inited_time = 1;
                                       session->last_pcr = pcr;
                                       session->last_time = now_time();
                                   }
                               }
                           } 
                       }
                    }

                    len = safe_send(w->fd, buffer, len);
                    if (len < 0)  {
                        if (errno == EAGAIN)
                            break;
                        else {
		                 ev_io_stop(work_loop, &session->ev_write);
		                 ev_io_stop(work_loop, &session->ev_read);
		                 ev_timer_stop (work_loop, &session->ev_time);
                            if (w->fd) {
                               close(w->fd);
                               session->fd = 0;
                            }
                            if (session->file_fd) {
                               close(session->file_fd);
                               session->file_fd = 0;
                            }
                            break;
                        }
                   }
                   if (has_pcr == 0){
                       break;
                   }
                   uint64_t now = now_time();
                   diff_time = now - session->last_time;
                   session->last_time = now;
                   if (diff_pcr < 0) {
                       session->play_duration += 40;
                   }
                   if (diff_time < 0) {
                       session->send_duration += 40;
                   }
                   session->play_duration += diff_pcr;
                   session->send_duration += diff_time;
                   interval = session->play_duration - session->send_duration;
                   if (interval <= 0) {
                       break;
                   }
                   if (interval >= 2) {
                       ev_io_stop(work_loop, w);
                       float c = interval / 1000.0;
                       ev_timer_set(&session->ev_time, c, 0.);
                       ev_timer_start (work_loop, &session->ev_time);
                   }
                  
                }
                else { //flv
                ////////////////////////////////////
                    Tag_s tag;
                    int64_t diff_time = 0;
                    int64_t diff_pcr = 0;
                    uint32_t has_pcr = 0;
                    ssize_t len = flv_read_tag(&tag, session->file_fd);
                    if (len <= 0) {
                        ///////////////////////////
                        close(session->file_fd);
                        struct tm start;
                        char timestr[64];
                        char fullpath[255];
                        session->start_time += 60;
                        localtime_r(&session->start_time, &start);
                        strftime(timestr, sizeof(timestr), "%Y%m%d%H%M.flv", &start);
                        sprintf(fullpath, "%s/%s/%s", session->g_path, session->name, timestr);
                        session->file_fd = open(fullpath, O_RDONLY);
                        printf("change file %s\n", fullpath);
                        if (session->file_fd < 0) {
                            session->start_time -= 60;
                            session->inited_time = 0;
                        }
                        break;
                    }
                    if (IS_VIDEO_TAG(tag)) {
                        has_pcr = 1;
                    if (session->inited_time > 0) {
                        diff_pcr =  tag.time_stamp - session->last_pcr;
                        session->last_pcr = tag.time_stamp;
                    } else {
                        //reinit
                        session->play_duration = 0;
                        session->send_duration = 0;
                        session->inited_time = 1;
                        session->last_time = now_time();
                        session->last_pcr = tag.time_stamp;
                    }
                    }

                    len = safe_send(w->fd, tag.data, 15 + len);
                    if (len < 0)  {
                        free(tag.data);
                        if (errno == EAGAIN)
                            break; 
                        else {
                            ev_io_stop(work_loop, w);
		            ev_io_stop(work_loop, &session->ev_read);
		            ev_timer_stop (work_loop, &session->ev_time);
                            if (session->file_fd) {
                               close(session->file_fd);
                               session->file_fd = 0;
                            }
                            if (w->fd) {
		                close(w->fd);
                                session->fd = 0;
                            }
                            break;
                        }
                   }
                   free(tag.data);
                   if (has_pcr == 0){
                       break;
                   }
                   uint64_t now = now_time();
                   diff_time = now - session->last_time;
                   session->last_time = now;
                   if (diff_pcr < 0) {
                       session->play_duration += 40;
                   }
                   if (diff_time < 0) {
                       session->send_duration += 40;
                   }

                   session->play_duration += diff_pcr;
                   session->send_duration += diff_time;
                   interval = session->play_duration - session->send_duration;
                   if (interval <= 0) {
                       break;
                   }
                   if (interval >= 2) {
                       ev_io_stop(work_loop, w);
                       float c = interval / 1000.0;
                       ev_timer_set(&session->ev_time, c, 0.);
                       ev_timer_start (work_loop, &session->ev_time);
                   }
                }
                break;
            }
            default:
                printf("state error\n");
        } 
         	//close(w->fd);
//	此处可以安装一个ev_timer ev_timer的回调中,再次安装ev_io write
}