Exemple #1
0
int main(int argc, char **argv) 
{
    int i = 0, rootid = 0, id = 0, j = 0, old = 0, data = 0, n = 0, count = 50000000;
    unsigned char digest[MD5_LEN];
    void *mmtree = NULL;
    void *timer = NULL;
    char line[1024];
    int64_t key = 0;

    if((mmtree = mmtree64_init("/tmp/test.mmtree64")))
    {
        rootid = mmtree64_new_tree(mmtree);
        TIMER_INIT(timer);
        for(j = 1; j <= count; j++)
        {
            n = sprintf(line, "http://www.demo.com/%d.html", j);
            md5(line, n, digest);
            key = *((int64_t *)digest);
            old = -1;
            data = j;
            id = mmtree64_insert(mmtree, rootid, key, data, &old);
            if(old > 0 || id <= 0) 
            {
                fprintf(stdout, "%d:{id:%d key:%d rootid:%d old:%d}\n", j, id, key, rootid, old);
            }
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "%s::%d insert:%d time:%lld\n", __FILE__,__LINE__, count, PT_LU_USEC(timer));
        for(j = 1; j <= count; j++)
        {
            n = sprintf(line, "http://www.demo.com/%d.html", j);
            md5(line, n, digest);
            key = *((int64_t *)digest);
            old = -1;
            data = j;
            id = mmtree64_try_insert(mmtree, rootid, key, data, &old);
            if(old > 0 && old != j) 
            {
                fprintf(stdout, "%d:{id:%d key:%d rootid:%d old:%d}\n", j, id, key, rootid, old);
                _exit(-1);
            }
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "%s::%d try_insert:%d time:%lld\n", __FILE__,__LINE__, count, PT_LU_USEC(timer));
        TIMER_CLEAN(timer);
        mmtree64_close(mmtree);
    }
}
Exemple #2
0
int http_show_state(int n)
{
    int nok = 0;
    TIMER_SAMPLE(timer);
    nok = n - ntimeouts;
    if(PT_USEC_U(timer) > 0 && nok  > 0)
    {
        if(is_quiet)
        {
            REALLOG(logger, "timeout:%d error:%d ok:%d total:%d "
                    "time used:%lld request per sec:%lld avg_time:%lld", 
                    ntimeouts, nerrors, nok, n, PT_USEC_U(timer), 
                    (((long long int)nok * 1000000ll)/PT_USEC_U(timer)),
                    (PT_USEC_U(timer)/nok));
        }
        else
        {
            fprintf(stdout, "timeout:%d error:%d ok:%d total:%d\n"
                    "time used:%lld request per sec:%lld avg_time:%lld\n", 
                    ntimeouts, nerrors, nok, n, PT_USEC_U(timer), 
                    ((long long int)nok * 1000000ll/PT_USEC_U(timer)),
                    (PT_USEC_U(timer)/nok));
        }
    }
    running_status = 0;sbase->stop(sbase);
    return 0;
}
Exemple #3
0
int main(int argc, char **argv)
{
    int i = 0, id = 0, n = 0, x = 0, count = 50000000;
    char line[FILE_LINE_MAX], *p = NULL;
    unsigned short *list = NULL;
    unsigned char digest[MD5_LEN];
    MMKEY *mmkey = NULL;
    void *timer = NULL;

    if((mmkey = mmkey_init("/tmp/test.mmkey")))
    {
        TIMER_INIT(timer);
        for(i = 1; i <= count; i++)
        {
            n = sprintf(line, "http://www.demo.com/%d.html", i);
            md5(line, n, digest);
            list = (unsigned short *)digest;
            id = mmkey_xadd(mmkey, list, 4);
            /*
            if(mmkey_get(mmkey, list, 4) != i)
            {
                fprintf(stderr, "NoFound key:%02X%02X%02X%02X id:%d\n", list[0],list[1],list[2],list[3], i);
                _exit(-1);
            }
            */
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "%s::%d insert:%d time:%lld\n", __FILE__,__LINE__, count, PT_LU_USEC(timer));
        TIMER_CLEAN(timer);
        mmkey->clean(mmkey);
    }
}
Exemple #4
0
int http_show_state(int n)
{
    TIMER_SAMPLE(timer);
    if(PT_USEC_U(timer) > 0 && ncompleted > 0)
    {
        if(is_quiet)
        {
            REALLOG(logger, "timeout:%d error:%d total:%d "
                    "time used:%lld request per sec:%lld avg_time:%lld", 
                    ntimeout, nerrors, ncompleted, PT_USEC_U(timer), 
                    ((long long int)ncompleted * 1000000ll/PT_USEC_U(timer)),
                    (PT_USEC_U(timer)/ncompleted));
        }
        else
        {
            fprintf(stdout, "timeout:%d error:%d total:%d\n"
                    "time used:%lld request per sec:%lld avg_time:%lld\n", 
                    ntimeout, nerrors, ncompleted, PT_USEC_U(timer), 
                    ((long long int)ncompleted * 1000000ll/PT_USEC_U(timer)),
                    (PT_USEC_U(timer)/ncompleted));
        }
    }
    if(is_daemon == 0){running_status = 0;sbase->stop(sbase);}
    return 0;
}
Exemple #5
0
/* read handler */
int conn_read_handler(CONN *conn)
{
    int ret = -1, n = -1;
    CONN_CHECK_RET(conn, ret);

    if(conn)
    {
        /* Receive OOB */
        if((n = MB_RECV(conn->oob, conn->fd, MSG_OOB)) > 0)
        {
            conn->recv_oob_total += n;
            DEBUG_LOGGER(conn->logger, "Received %d bytes OOB total %lld from %s:%d via %d",
                    n, conn->recv_oob_total, conn->ip, conn->port, conn->fd);
            conn->oob_handler(conn);
            /* CONN TIMER sample */
            TIMER_SAMPLE(conn->timer);
            return (ret = 0);
        }
        /* Receive to chunk with chunk_read_state before reading to buffer */
        CONN_CHUNK_READ(conn, n);
        /* Receive normal data */
        if((n = MB_READ(conn->buffer, conn->fd)) <= 0)
        {
            FATAL_LOGGER(conn->logger, "Reading %d bytes left:%d data from %s:%d via %d failed, %s",
                    n, MB_LEFT(conn->buffer), conn->ip, conn->port, conn->fd, strerror(errno));
            /* Terminate connection */
            CONN_TERMINATE(conn);
            return (ret = 0);
        }
        /* CONN TIMER sample */
        TIMER_SAMPLE(conn->timer);
        conn->recv_data_total += n;
        DEBUG_LOGGER(conn->logger, "Received %d bytes data total %lld from %s:%d via %d",
                n, conn->recv_data_total, conn->ip, conn->port, conn->fd);
        conn->packet_reader(conn);
        ret = 0;
    }
    return ret;
}
Exemple #6
0
/* write handler */
int conn_write_handler(CONN *conn)
{
    int ret = -1, n = 0;
    CONN_CHECK_RET(conn, ret);
    CHUNK *cp = NULL;

    if(conn && conn->send_queue && QTOTAL(conn->send_queue) > 0)
    {
        DEBUG_LOGGER(conn->logger, "Ready for send data to %s:%d via %d "
                "qtotal:%d qhead:%d qcount:%d",
                conn->ip, conn->port, conn->fd, QTOTAL(conn->send_queue),
                QHEAD(conn->send_queue), QCOUNT(conn->send_queue));   
        if(QUEUE_HEAD(conn->send_queue, PCHUNK, &cp) == 0)
        {
            DEBUG_LOGGER(conn->logger, "Ready for send data to %s:%d via %d qtotal:%d pcp:%08x",
                    conn->ip, conn->port, conn->fd, QTOTAL(conn->send_queue), cp);   
            if((n = CHUNK_WRITE(cp, conn->fd)) > 0)
            {
                conn->sent_data_total += n;
                DEBUG_LOGGER(conn->logger, "Sent %d byte(s) (total sent %lld) "
                        "to %s:%d via %d leave %lld", n, conn->sent_data_total, 
                        conn->ip, conn->port, conn->fd, CK_LEFT(cp));
                /* CONN TIMER sample */
                TIMER_SAMPLE(conn->timer);
                if(CHUNK_STATUS(cp) == CHUNK_STATUS_OVER )
                {
                    if(QUEUE_POP(conn->send_queue, PCHUNK, &cp) == 0)
                    {
                        DEBUG_LOGGER(conn->logger, "Completed chunk[%08x] and clean it leave %d",
                                cp, QTOTAL(conn->send_queue));
                        CK_CLEAN(cp);
                    }
                }
                ret = 0;
            }
            else
            {
                FATAL_LOGGER(conn->logger, "Sending data to %s:%d via %d failed, %s",
                        conn->ip, conn->port, conn->fd, strerror(errno));
                /* Terminate connection */
                CONN_TERMINATE(conn);
            }
        }
        if(QTOTAL(conn->send_queue) <= 0)
        {
            conn->event->del(conn->event, E_WRITE);
        }
    }
    return ret;
}
Exemple #7
0
int main()
{
    int i = 0, key = 0, count = 10000, ret = 0;
    void *map = NULL, *timer = NULL;

    if((map = IMMX_INIT()))
    {
        TIMER_INIT(timer);
        while(i++ < count)
        {
            key = random()%count;
            IMMX_ADD(map, key);
            //if(olddp)fprintf(stdout, "old[%d:%08x]\n", key, olddp);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "insert %d nodes count:%d free:%d total:%d time used:%lld\n", count,  PIMX(map)->count, PIMX(map)->free_count, PIMX(map)->total, PT_LU_USEC(timer));
        i = 0;
        while(i++ < count)
        {
            key = random()%count;
            ret = 0;
            IMMX_GET(map, key, ret);
            if(ret) fprintf(stdout, "%d:[%d]\n", key, ret);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "get %d nodes count:%d free:%d total:%d time used:%lld\n", count,  PIMX(map)->count, PIMX(map)->free_count, PIMX(map)->total, PT_LU_USEC(timer));
        do
        {
            ret = 0;
            IMMX_POP_MIN(map, key, ret);
            if(ret) fprintf(stdout, "%d:[%d]\n", key, ret); 
        }while(IMX_ROOT(map));
        TIMER_SAMPLE(timer);
        fprintf(stdout, "pop min(%d) node count:%d free:%d total:%d time used:%lld\n", count,  PIMX(map)->count, PIMX(map)->free_count, PIMX(map)->total, PT_LU_USEC(timer));
        i = 0;
        while(i < count)
        {
            key = random()%count;
            IMMX_ADD(map, key);
            ++i;
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "insert %d nodes count:%d free:%d total:%d time used:%lld\n", count,  PIMX(map)->count, PIMX(map)->free_count, PIMX(map)->total, PT_LU_USEC(timer));
        do
        {
            ret = 0;
            IMMX_POP_MAX(map, key, ret);
            if(ret) fprintf(stdout, "%d:[%d]\n", key, ret); 
        }while(IMX_ROOT(map));
        TIMER_SAMPLE(timer);
        fprintf(stdout, "pop max(%d) nodes count:%d free:%d total:%d time used:%lld\n", count,  PIMX(map)->count, PIMX(map)->free_count, PIMX(map)->total, PT_LU_USEC(timer));
        TIMER_SAMPLE(timer);
        TIMER_CLEAN(timer);
        IMMX_CLEAN(map);
    }
}
Exemple #8
0
/* sock_dgram /UCP handler */
void ev_udp_handler(int fd, int ev_flags, void *arg)
{
    int i = 0, n = 0, nevdnsbuf = 0, reqfd = -1, respfd = -1;
    unsigned char evdnsbuf[EVDNS_BUF_SIZE], *p = NULL;
    char ip[DNS_IP_MAX];
    HOSTENT hostent = {0};

    if(ev_flags & E_READ)
    {
        //if( ( n = recvfrom(fd, buffer[fd], EV_BUF_SIZE, 0, (struct sockaddr *)&rsa, &rsa_len)) > 0 )
        if( ( n = read(fd, buffer[fd], EV_BUF_SIZE)) > 0 )
        {
            TIMER_SAMPLE(timer);
            evdns_parse_reply((unsigned char *)buffer[fd], n, &hostent);
            if(hostent.naddrs  > 0)
            {
                fprintf(stdout, "time:%lld QID:%d name:%s nalias:%d naddrs:%d\n", 
                        PT_LU_USEC(timer), hostent.qid, hostent.name, 
                        hostent.nalias, hostent.naddrs);
                for(i = 0; i < hostent.nalias; i++)
                {
                    fprintf(stdout, "alias%d:%s\n", i, hostent.alias[i]);
                }
                for(i = 0; i < hostent.naddrs; i++)
                {
                    p = (unsigned char *)&(hostent.addrs[i]);
                    sprintf(ip, "%d.%d.%d.%d", *p, *(p+1), *(p+2), *(p+3));
                    fprintf(stdout, "ip[%d] [%d][%s:][%d]\n", 
                            i, hostent.addrs[i], ip, inet_addr(ip));
                }
                fprintf(stdout, "\r\n");
            }
            if((respfd = open("/tmp/dns.response", O_CREAT|O_RDWR, 0644)) > 0)
            {
                if(write(respfd, buffer[fd], n) <= 0)_exit(-1);
                close(respfd);
                respfd = -1;
                _exit(-1);
            }
            SHOW_LOG("Read %d bytes from %d", n, fd);
            buffer[fd][n] = 0;
            SHOW_LOG("Updating event[%p] on %d ", &events[fd], fd);
            event_add(&events[fd], E_WRITE);	
        }		
        else
        {
            if(n < 0 )
                FATAL_LOG("Reading from %d failed, %s", fd, strerror(errno));
            goto err;
        }
    }
    if(ev_flags & E_WRITE)
    {
        nevdnsbuf = evdns_make_query(domain, 1, 1, 1, 1, evdnsbuf);
        if((reqfd = open("/tmp/dns.query", O_CREAT|O_RDWR, 0644)) > 0)
        {
            if(write(reqfd, evdnsbuf, nevdnsbuf) <= 0)_exit(-1);
            close(reqfd);
            reqfd = -1;
        }
        //if(  (n = write(fd, buffer[fd], strlen(buffer[fd])) ) > 0 )
        if((n = write(fd, evdnsbuf, nevdnsbuf)) > 0)
        {
            SHOW_LOG("Wrote %d bytes via %d", n, fd);
            TIMER_RESET(timer);
        }
        else
        {
            if(n < 0)
                FATAL_LOG("Wrote data via %d failed, %s", fd, strerror(errno));	
            goto err;
        }
        event_del(&events[fd], E_WRITE);
    }
    return ;
err:
    {
        event_destroy(&events[fd]);
        shutdown(fd, SHUT_RDWR);
        close(fd);
        SHOW_LOG("Connection %d closed", fd);
    }
}
Exemple #9
0
int main(int argc, char **argv)
{
    unsigned char url[HTTP_URL_MAX], *p = NULL;
    FILE *fp = NULL;
    unsigned char  key[MD5_LEN];
    void *urlmap = NULL, *dp = NULL, *olddp = NULL;
    long n = 0, id = 0;
    void *timer = NULL;
    long long total = 0;
    char *file = NULL;

    if(argc < 2)
    {
        fprintf(stderr, "Usage:%s file\n", argv[0]);
        _exit(-1);
    }
    file = argv[1];
    if(file && (fp = fopen(file, "r")))
    {
        KVMAP_INIT(urlmap);
        if(urlmap)
        {
            TIMER_INIT(timer);
            while(fgets((char *)url, HTTP_URL_MAX, fp))
            {
                //fprintf(stdout, "%s", url);
                //memset(url, 0, HTTP_URL_MAX);
                //continue;
                //continue;
                p = url;
                while(*p != '\0' && *p != '\r' && *p != '\n') ++p;
                n = p - url;
                //n = strlen(url) - 1;
                //if(n == 0) {fprintf(stdout, "URL:%d\n", id); continue;}
                md5(url, n, key);
                dp = (void *)++id;
                olddp = NULL;
                TIMER_SAMPLE(timer);
                KVMAP_ADD(urlmap, key, dp, olddp);
                TIMER_SAMPLE(timer);
                total += PT_LU_USEC(timer);
                if(olddp)
                {
                    fprintf(stdout, "Repeat:%ld vs %ld:%d:%s\n", 
                            (long)dp, (long)olddp, n, url);
                }
                else
                {
                    //fprintf(stdout, "%ld:%s", id, url);
                }
                memset(url, 0, HTTP_URL_MAX);
            }
            if(id > 0)
            {
                fprintf(stdout, "add %d sizeof(KVNODE):%d avg:%g microsecond(s)\n", 
                        id, sizeof(KVNODE), (double )(total/id));
            }
            TIMER_CLEAN(timer);
            KVMAP_CLEAN(urlmap);
        }
        fclose(fp);
    }
    while(1) sleep(1);
}
Exemple #10
0
void aks_to_M2(
  kiss_fft_cfg  fft_fwd_cfg,
  float         ak[],	     /* LPC's */
  int           order,
  MODEL        *model,	     /* sinusoidal model parameters for this frame */
  float         E,	     /* energy term */
  float        *snr,	     /* signal to noise ratio for this frame in dB */
  int           dump,        /* true to dump sample to dump file */
  int           sim_pf,      /* true to simulate a post filter */
  int           pf,          /* true to LPC post filter */
  int           bass_boost,  /* enable LPC filter 0-1khz 3dB boost */
  float         beta,
  float         gamma        /* LPC post filter parameters */
)
{
  COMP pw[FFT_ENC];	/* input to FFT for power spectrum */
  COMP Pw[FFT_ENC];	/* output power spectrum */
  int i,m;		/* loop variables */
  int am,bm;		/* limits of current band */
  float r;		/* no. rads/bin */
  float Em;		/* energy in band */
  float Am;		/* spectral amplitude sample */
  float signal, noise;
  TIMER_VAR(tstart, tfft, tpw, tpf);

  TIMER_SAMPLE(tstart);

  r = TWO_PI/(FFT_ENC);

  /* Determine DFT of A(exp(jw)) --------------------------------------------*/

  for(i=0; i<FFT_ENC; i++) {
    pw[i].real = 0.0;
    pw[i].imag = 0.0;
  }

  for(i=0; i<=order; i++)
    pw[i].real = ak[i];
  kiss_fft(fft_fwd_cfg, (kiss_fft_cpx *)pw, (kiss_fft_cpx *)Pw);

  TIMER_SAMPLE_AND_LOG(tfft, tstart, "      fft");

  /* Determine power spectrum P(w) = E/(A(exp(jw))^2 ------------------------*/

  for(i=0; i<FFT_ENC/2; i++)
    Pw[i].real = E/(Pw[i].real*Pw[i].real + Pw[i].imag*Pw[i].imag);

  TIMER_SAMPLE_AND_LOG(tpw, tfft, "      Pw");

  if (pf)
      lpc_post_filter(fft_fwd_cfg, model, Pw, ak, order, dump, beta, gamma, bass_boost);

  TIMER_SAMPLE_AND_LOG(tpf, tpw, "      LPC post filter");

  #ifdef DUMP
  if (dump)
      dump_Pw(Pw);
  #endif

  /* Determine magnitudes from P(w) ----------------------------------------*/

  /* when used just by decoder {A} might be all zeroes so init signal
     and noise to prevent log(0) errors */

  signal = 1E-30; noise = 1E-32;

  for(m=1; m<=model->L; m++) {
      am = (int)((m - 0.5)*model->Wo/r + 0.5);
      bm = (int)((m + 0.5)*model->Wo/r + 0.5);
      Em = 0.0;

      for(i=am; i<bm; i++)
          Em += Pw[i].real;
      Am = sqrtf(Em);

      signal += model->A[m]*model->A[m];
      noise  += (model->A[m] - Am)*(model->A[m] - Am);

      /* This code significantly improves perf of LPC model, in
         particular when combined with phase0.  The LPC spectrum tends
         to track just under the peaks of the spectral envelope, and
         just above nulls.  This algorithm does the reverse to
         compensate - raising the amplitudes of spectral peaks, while
         attenuating the null.  This enhances the formants, and
         supresses the energy between formants. */

      if (sim_pf) {
          if (Am > model->A[m])
              Am *= 0.7;
          if (Am < model->A[m])
              Am *= 1.4;
      }

      model->A[m] = Am;
  }
  *snr = 10.0*log10f(signal/noise);

  TIMER_SAMPLE_AND_LOG2(tpf, "      rec");
}
Exemple #11
0
void lpc_post_filter(kiss_fft_cfg fft_fwd_cfg, MODEL *model, COMP Pw[], float ak[],
                     int order, int dump, float beta, float gamma, int bass_boost)
{
    int   i;
    COMP  x[FFT_ENC];   /* input to FFTs                */
    COMP  Aw[FFT_ENC];  /* LPC analysis filter spectrum */
    COMP  Ww[FFT_ENC];  /* weighting spectrum           */
    float Rw[FFT_ENC];  /* R = WA                       */
    float e_before, e_after, gain;
    float Pfw[FFT_ENC]; /* Post filter mag spectrum     */
    float max_Rw, min_Rw;
    float coeff;
    TIMER_VAR(tstart, tfft1, taw, tfft2, tww, tr);

    TIMER_SAMPLE(tstart);

    /* Determine LPC inverse filter spectrum 1/A(exp(jw)) -----------*/

    /* we actually want the synthesis filter A(exp(jw)) but the
       inverse (analysis) filter is easier to find as it's FIR, we
       just use the inverse of 1/A to get the synthesis filter
       A(exp(jw)) */

    for(i=0; i<FFT_ENC; i++) {
	x[i].real = 0.0;
	x[i].imag = 0.0;
    }

    for(i=0; i<=order; i++)
	x[i].real = ak[i];
    kiss_fft(fft_fwd_cfg, (kiss_fft_cpx *)x, (kiss_fft_cpx *)Aw);

    TIMER_SAMPLE_AND_LOG(tfft1, tstart, "        fft1");

    for(i=0; i<FFT_ENC/2; i++) {
	Aw[i].real = 1.0/(Aw[i].real*Aw[i].real + Aw[i].imag*Aw[i].imag);
    }

    TIMER_SAMPLE_AND_LOG(taw, tfft1, "        Aw");

    /* Determine weighting filter spectrum W(exp(jw)) ---------------*/

    for(i=0; i<FFT_ENC; i++) {
	x[i].real = 0.0;
	x[i].imag = 0.0;
    }

    x[0].real = ak[0];
    coeff = gamma;
    for(i=1; i<=order; i++) {
	x[i].real = ak[i] * coeff;
        coeff *= gamma;
    }
    kiss_fft(fft_fwd_cfg, (kiss_fft_cpx *)x, (kiss_fft_cpx *)Ww);

    TIMER_SAMPLE_AND_LOG(tfft2, taw, "        fft2");

    for(i=0; i<FFT_ENC/2; i++) {
	Ww[i].real = Ww[i].real*Ww[i].real + Ww[i].imag*Ww[i].imag;
    }

    TIMER_SAMPLE_AND_LOG(tww, tfft2, "        Ww");

    /* Determined combined filter R = WA ---------------------------*/

    max_Rw = 0.0; min_Rw = 1E32;
    for(i=0; i<FFT_ENC/2; i++) {
	Rw[i] = sqrtf(Ww[i].real * Aw[i].real);
	if (Rw[i] > max_Rw)
	    max_Rw = Rw[i];
	if (Rw[i] < min_Rw)
	    min_Rw = Rw[i];

    }

    TIMER_SAMPLE_AND_LOG(tr, tww, "        R");

    #ifdef DUMP
    if (dump)
      dump_Rw(Rw);
    #endif

    /* create post filter mag spectrum and apply ------------------*/

    /* measure energy before post filtering */

    e_before = 1E-4;
    for(i=0; i<FFT_ENC/2; i++)
	e_before += Pw[i].real;

    /* apply post filter and measure energy  */

    #ifdef DUMP
    if (dump)
	dump_Pwb(Pw);
    #endif

    e_after = 1E-4;
    for(i=0; i<FFT_ENC/2; i++) {
	Pfw[i] = powf(Rw[i], beta);
	Pw[i].real *= Pfw[i] * Pfw[i];
	e_after += Pw[i].real;
    }
    gain = e_before/e_after;

    /* apply gain factor to normalise energy */

    for(i=0; i<FFT_ENC/2; i++) {
	Pw[i].real *= gain;
    }

    if (bass_boost) {
        /* add 3dB to first 1 kHz to account for LP effect of PF */

        for(i=0; i<FFT_ENC/8; i++) {
            Pw[i].real *= 1.4*1.4;
        }
    }

    TIMER_SAMPLE_AND_LOG2(tr, "        filt");
}
Exemple #12
0
float nlp(
  void *nlp_state, 
  float  Sn[],			/* input speech vector */
  int    n,			/* frames shift (no. new samples in Sn[]) */
  int    pmin,                  /* minimum pitch value */
  int    pmax,			/* maximum pitch value */
  float *pitch,			/* estimated pitch period in samples */
  COMP   Sw[],                  /* Freq domain version of Sn[] */
  COMP   W[],                   /* Freq domain window */
  float *prev_Wo
)
{
    NLP   *nlp;
    float  notch;		    /* current notch filter output    */
    COMP   fw[PE_FFT_SIZE];	    /* DFT of squared signal (input)  */
    COMP   Fw[PE_FFT_SIZE];	    /* DFT of squared signal (output) */
    float  gmax;
    int    gmax_bin;
    int    m, i,j;
    float  best_f0;
    TIMER_VAR(start, tnotch, filter, peakpick, window, fft, magsq, shiftmem);
    
    assert(nlp_state != NULL);
    nlp = (NLP*)nlp_state;
    m = nlp->m;

    TIMER_SAMPLE(start);

    /* Square, notch filter at DC, and LP filter vector */

    for(i=m-n; i<m; i++) 	    /* square latest speech samples */
	nlp->sq[i] = Sn[i]*Sn[i];

    for(i=m-n; i<m; i++) {	/* notch filter at DC */
	notch = nlp->sq[i] - nlp->mem_x;
	notch += COEFF*nlp->mem_y;
	nlp->mem_x = nlp->sq[i];
	nlp->mem_y = notch;
	nlp->sq[i] = notch + 1.0;  /* With 0 input vectors to codec,
				      kiss_fft() would take a long
				      time to execute when running in
				      real time.  Problem was traced
				      to kiss_fft function call in
				      this function. Adding this small
				      constant fixed problem.  Not
				      exactly sure why. */
    }

    TIMER_SAMPLE_AND_LOG(tnotch, start, "      square and notch");

    for(i=m-n; i<m; i++) {	/* FIR filter vector */

	for(j=0; j<NLP_NTAP-1; j++)
	    nlp->mem_fir[j] = nlp->mem_fir[j+1];
	nlp->mem_fir[NLP_NTAP-1] = nlp->sq[i];

	nlp->sq[i] = 0.0;
	for(j=0; j<NLP_NTAP; j++)
	    nlp->sq[i] += nlp->mem_fir[j]*nlp_fir[j];
    }

    TIMER_SAMPLE_AND_LOG(filter, tnotch, "      filter");
 
    /* Decimate and DFT */

    for(i=0; i<PE_FFT_SIZE; i++) {
	fw[i].real = 0.0;
	fw[i].imag = 0.0;
    }
    for(i=0; i<m/DEC; i++) {
	fw[i].real = nlp->sq[i*DEC]*nlp->w[i];
    }
    TIMER_SAMPLE_AND_LOG(window, filter, "      window");
    #ifdef DUMP
    dump_dec(Fw);
    #endif

    kiss_fft(nlp->fft_cfg, (kiss_fft_cpx *)fw, (kiss_fft_cpx *)Fw);
    TIMER_SAMPLE_AND_LOG(fft, window, "      fft");

    for(i=0; i<PE_FFT_SIZE; i++)
	Fw[i].real = Fw[i].real*Fw[i].real + Fw[i].imag*Fw[i].imag;

    TIMER_SAMPLE_AND_LOG(magsq, fft, "      mag sq");
    #ifdef DUMP
    dump_sq(nlp->sq);
    dump_Fw(Fw);
    #endif

    /* find global peak */

    gmax = 0.0;
    gmax_bin = PE_FFT_SIZE*DEC/pmax;
    for(i=PE_FFT_SIZE*DEC/pmax; i<=PE_FFT_SIZE*DEC/pmin; i++) {
	if (Fw[i].real > gmax) {
	    gmax = Fw[i].real;
	    gmax_bin = i;
	}
    }
    
    TIMER_SAMPLE_AND_LOG(peakpick, magsq, "      peak pick");

    //#define POST_PROCESS_MBE
    #ifdef POST_PROCESS_MBE
    best_f0 = post_process_mbe(Fw, pmin, pmax, gmax, Sw, W, prev_Wo);
    #else
    best_f0 = post_process_sub_multiples(Fw, pmin, pmax, gmax, gmax_bin, prev_Wo);
    #endif

    TIMER_SAMPLE_AND_LOG(shiftmem, peakpick,  "      post process");

    /* Shift samples in buffer to make room for new samples */

    for(i=0; i<m-n; i++)
	nlp->sq[i] = nlp->sq[i+n];

    /* return pitch and F0 estimate */

    *pitch = (float)SAMPLE_RATE/best_f0;

    TIMER_SAMPLE_AND_LOG2(shiftmem,  "      shift mem");

    TIMER_SAMPLE_AND_LOG2(start,  "      nlp int");

    return(best_f0);  
}
Exemple #13
0
//rm -rf /tmp/1.idx* && gcc -O2 -o lkv lkv.c -DLKV_TEST -DTEST_KV -DHAVE_PTHREAD -lpthread && ./lkv
int main()
{
    LKV *lkv = NULL;
    int i = 0, j = 0, n = 0, total = 0, no = 0, stat[MASK], stat2[MASK],
        v = 0, num = 1000000, base = 60000000;
    int64_t val = 0, from = 0, to = 0, *res = NULL, all_mask = 200000;
    int64_t inputs[256], nos[256], last[256], tall[200000];
    int64_t all = 0;
    time_t stime = 0, etime = 0;
    void *timer = NULL;

    if((lkv = lkv_init("/tmp/1.idx")))
    {
        res = (int64_t *)calloc(base, sizeof(int64_t));
        TIMER_INIT(timer);
#ifdef TEST_KV
        TIMER_RESET(timer);
        for(i = 0; i < num; i++)
        {
            no = random()%base;
            LKV_SET(lkv, no, i);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "set(%d) time used:%lld\n", num, PT_LU_USEC(timer));
        TIMER_RESET(timer);
        for(i = 0; i < num; i++)
        {
            no = random()%base;
            v = LKV_GET(lkv, no);
        }
        fprintf(stdout, "get(%d) time used:%lld\n", num, PT_LU_USEC(timer));
#endif

#ifdef TEST_RFROM
            lkv_set(lkv, 1, 22);
            lkv_set(lkv, 2, 25);
            lkv_set(lkv, 3, 20);
            lkv_set(lkv, 4, 13);
            lkv_set(lkv, 6, 22);
            n = lkv_rangefrom(lkv, 21, NULL);
            fprintf(stdout, "rangefrom(21) => %d\n", n);
#endif
#ifdef TEST_DEB
        /*
            n = lkv_in(lkv, 16615, NULL);
            fprintf(stdout, "16615:%d\n", n);
        */
        n = 0;
        for(i = 0; i < lkv->state->count; i++)
        {
            /*
            if(lkv->slots[i].min <= 26650 && lkv->slots[i].max >= 26650)
            {
                fprintf(stdout, "%d:[min:%d max:%d]\n", i, lkv->slots[i].min, lkv->slots[i].max);
            }
            */
            n+= lkv->slots[i].count;
        }
        fprintf(stdout, "total:%d\n", n);
#endif
#ifdef TEST_IN
        for(i = 0; i < all_mask; i++)
        {
            tall[i] = 0;
        }
        for(i = 80000000; i > 0; i--)
        {
            no = (rand()%all_mask);
            lkv_set(lkv, i, no);
            tall[no]++;
        }
        for(i = 0; i < all_mask; i++)
        {
            n = lkv_in(lkv, i, NULL);
            if(n != tall[i])
                fprintf(stdout, "%d:[%d/%d]\n", i, n, tall[i]);
        }
#endif
#ifdef TEST_ALL
        for(i = 0; i < all_mask; i++)
        {
            tall[i] = 0;
        }
        for(i = 80000000; i > 0; i--)
        {
            no = (rand()%all_mask);
            lkv_set(lkv, i, no);
            tall[no]++;
        }
        no = (rand()%all_mask);
        total = 0;for(i = no; i < all_mask; i++) total += tall[i];
        fprintf(stdout, "rangefrom(%d):%d/%d\n", no, lkv_rangefrom(lkv, no, NULL), total);
        total = 0;for(i = 0; i <= no; i++) total += tall[i];
        fprintf(stdout, "rangeto(%d):%d/%d\n", no, lkv_rangeto(lkv, no, NULL), total);
        from = (rand()%all_mask);
        to = (rand()%all_mask);
        if(from > to){from += to; to = from - to; from = from - to;}
        total = 0;for(i = from; i <= to; i++) total += tall[i];
        fprintf(stdout, "range(%d,%d):%d/%d\n", from, no, lkv_range(lkv, from, to, NULL), total);
        for(i = 0; i < all_mask; i++)
        {
            n = lkv_in(lkv, i, NULL);
            if(n != tall[i])
                fprintf(stdout, "%d:[%d/%d]\n", i, n, tall[i]);
        }
#endif
#ifdef TEST_INS
        //fprintf(stdout, "sizeof(stat):%d\n", sizeof(stat));
        memset(stat, 0, sizeof(stat));
        memset(stat2, 0, sizeof(stat2));
        srand(time(NULL));
        n = 256;
        for(i = 0; i < n; i++)
        {
            no = (rand()%MASK);
            nos[i] = no;
            if((i % 3) == 0)
                inputs[i] = no * -1;
            else
                inputs[i] = no;
        }
        TIMER_RESET(timer);
        for(i = 1; i < 20000000; i++)
        {
           j = (rand()%n);
           val = inputs[j];
           no = nos[j];
           stat[no]++;
           lkv_set(lkv, i, val);
           last[j] = i;
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "set() 40000000 data, time used:%lld\n", PT_LU_USEC(timer));
        TIMER_RESET(timer);
        for(i = 0; i < n; i++)
        {
            lkv_del(lkv, last[i]);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "del() time used:%lld\n", PT_LU_USEC(timer));
        TIMER_RESET(timer);
        for(i = 0; i < n; i++)
        {
            val = inputs[i];
            no = nos[i];
            stat2[no] = lkv_in(lkv, val, res);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "in() time used:%lld\n", PT_LU_USEC(timer));
        TIMER_RESET(timer);
        total = lkv_ins(lkv, inputs, n, NULL);
        TIMER_SAMPLE(timer);
        fprintf(stdout, "ins(keys, NULL) total:%d time used:%lld\n", total, PT_LU_USEC(timer));
        TIMER_RESET(timer);
        total = lkv_ins(lkv, inputs, n, res);
        TIMER_SAMPLE(timer);
        fprintf(stdout, "ins(keys, res:%p) total:%d time used:%lld\n", res, total, PT_LU_USEC(timer));
        for(i = 0; i < n; i++)
        {
            j = nos[i];
            if(stat[j] != stat2[j])
                fprintf(stdout, "%d:%d/%d::%d\n", j, stat[j], stat2[j], inputs[i]);
        }
#ifdef OUT_ALL
        for(i = 0; i < total; i++)
        {
            fprintf(stdout, "%d:%d\n", i, res[i]);
        }
#endif
#endif
        /*
        for(i = 0; i < lkv->state->count; i++)
        {
            fprintf(stdout, "%d:{min:%d max:%d}(%d)\n", i, lkv->slots[i].min, lkv->slots[i].max, lkv->slots[i].count);
        }
        */
#ifdef TEST_RANGEFILTER
            lkv_set(lkv, 1, 1234567);
            lkv_set(lkv, 2, 1567890);
            fprintf(stdout, "rangefrom():%d\n", lkv_rangefrom(lkv, 1569000, NULL));
            fprintf(stdout, "rangeto():%d\n", lkv_rangeto(lkv, 1111111, NULL));
            fprintf(stdout, "range():%d\n", lkv_range(lkv, 1111111, 1400000, NULL));
#endif
#ifdef TEST_RANGE
        srand(time(NULL));
        TIMER_RESET(timer);
        for(i = 1; i < 40000000; i++)
        {
            val = 1356969600 + (rand()%31536000);
            lkv_set(lkv, i, val);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "set() 40000000 timestamps,  time used:%lld\n", PT_LU_USEC(timer));
        srand(time(NULL));
        TIMER_RESET(timer);
        all = 0;
        for(i = 0; i < 1000; i++)
        {
            val = 1356969600 + (rand()%31536000);
            all += lkv_rangefrom(lkv, val, res);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "rangefrom() 1000 times total:%lld, time used:%lld\n", (long long int)all, PT_LU_USEC(timer));
        srand(time(NULL));
        TIMER_RESET(timer);
        all = 0;
        for(i = 0; i < 1000; i++)
        {
            val = 1356969600 + (rand()%31536000);
            all += lkv_rangeto(lkv, val, res);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "rangeto() 1000 times total:%lld, time used:%lld\n", (long long int)all, PT_LU_USEC(timer));
        srand(time(NULL));
        TIMER_RESET(timer);
        all = 0;
        for(i = 0; i < 1000; i++)
        {
            from = 1356969600 + (rand()%31536000);
            to = from + rand()%31536000;
            all += lkv_range(lkv, from, to, res);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "range(%p) 1000 times total:%lld, time used:%lld\n", res, (long long int)all, PT_LU_USEC(timer));
        srand(time(NULL));
        TIMER_RESET(timer);
        all = 0;
        for(i = 0; i < 1000; i++)
        {
            from = 1356969600 + (rand()%31536000);
            to = from + rand()%31536000;
            all += lkv_range(lkv, from, to, NULL);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "range(null) 1000 times total:%lld, time used:%lld\n", (long long int)all, PT_LU_USEC(timer));

#endif
        lkv_close(lkv);
        TIMER_CLEAN(timer);
        free(res);
    }
}
Exemple #14
0
int main(int argc, char **argv)
{
    int i = 0, k = 0, x = 0, mid = 0, qid = 0, port = 0, flag = 0, dbport = 0, 
        isdaemon = 0, isout = 0, packetid = 0, ret = -1, from = 0, to = 0;
    char tmp[XX_BUF_SIZE], url[XX_URL_MAX],*ip = NULL, *dbhost = NULL, 
         block[XX_PACKET_MAX * sizeof(int64_t)], ch = 0;
    int64_t *list = NULL, key = 0, rand = 0;
    unsigned char digest[MD5_LEN];
    BJSON request = {0};
    void *timer = NULL;
    BREC record = {0};
    MTASK mtask = {0};
    pid_t pid = 0;
    DBASE db = {0};

    /* get configure file */
    while((ch = getopt(argc, argv, "h:p:m:q:i:x:n:c:f:t:od")) != -1)
    {
        switch(ch)
        {
            case 'h':
                ip = optarg;
                break;
            case 'p':
                port = atoi(optarg);
                break;
            case 'm':
                mid = atoi(optarg);
                break;
            case 'q':
                qid = atoi(optarg);
                break;
            case 'i':
                dbhost = optarg;
                break;
            case 'x':
                dbport = atoi(optarg);
                break;
            case 'f':
                from = atoi(optarg);
                break;
            case 't':
                to = atoi(optarg);
                break;
            case 'o':
                isout = 1;
                break;
            case 'd':
                isdaemon = 1;
                break;
            default:
                break;
            }
    }
    if(ip == NULL || port <= 0 || (qid == 0 && mid == 0) || dbhost == NULL || dbport <= 0)
    {
        fprintf(stderr, "Usage:%s -h qtask_host -p qtask_port -m commitid -q queueid "
                "-i dbhost -x dbport -f from -t to -o output -d is_working_daemon\n", argv[0]);
        _exit(-1);
    }
    /* daemon */
    if(isdaemon)
    {
        pid = fork();
        switch (pid) {
            case -1:
                perror("fork()");
                exit(EXIT_FAILURE);
                break;
            case 0: /* child process */
                if(setsid() == -1)
                    exit(EXIT_FAILURE);
                break;
            default:/* parent */
                _exit(EXIT_SUCCESS);
                break;
        }
    }
    if(mtask_set(&mtask, ip, port, mid, qid) == 0 
            && mtask_connect(&mtask) == 0)
    {
        if(dbase_set(&db, dbhost, dbport) != 0 || dbase_connect(&db) != 0)
        {
            fprintf(stderr, "connect to db[%s:%d] failed, %s\n", dbhost, dbport, strerror(errno));
            _exit(-1);
        }
        list = (int64_t *)block;
        //sprintf(ns, "%s.%s", dbname, collection);
        TIMER_INIT(timer);
        BJSON_INIT(request);
        k = from;
        if(to <= 0) to = from + 10000000;
        do
        {
            TIMER_SAMPLE(timer);
            rand = random();
            flag = 0;
            //if((rand%33) == 0) flag = MTASK_TO_QHEAD;
            x = 0;
            while(x < XX_PACKET_MAX)
            {
                //fprintf(stdout, "%s::%d ns:%s OK\n", __FILE__, __LINE__, ns);
                sprintf(url, "http://demo.com/%d.html", ++k);
                memset(digest, 0, MD5_LEN);
                md5((unsigned char *)url, strlen(url), digest);
                key = *((int64_t *)digest);
                sprintf(tmp, "title %d content %d comment %d", k, k, k);
                brequest_reset(&request);
                bjson_start(&request);
                bjson_append_long(&request, "id", key);
                bjson_new_object(&request, "property");
                bjson_append_int(&request, "status", 1);
                bjson_append_int(&request, "crc", crc32(tmp, strlen(tmp)));
                bjson_append_int(&request, "slevel", 1);
                bjson_append_long(&request, "category", (int64_t)1 << (rand%64));
                bjson_append_double(&request, "rank", 1.0);
                bjson_finish_object(&request);
                //fprintf(stdout, "%s::%d key:%lld\n", __FILE__, __LINE__, key);
                bjson_new_object(&request, "text_index");
                sprintf(tmp, "title %d", k);
                bjson_append_string(&request, "title", tmp);
                sprintf(tmp, "content %d", k);
                bjson_append_string(&request, "content", tmp);
                sprintf(tmp, "comment %d", k);
                bjson_append_string(&request, "comment", tmp);
                bjson_finish_object(&request);
                //fprintf(stdout, "%s::%d key:%lld\n", __FILE__, __LINE__, key);
                bjson_new_object(&request, "int_index");
                bjson_append_int(&request, "0", rand/3);
                bjson_append_int(&request, "1", rand/9);
                bjson_append_int(&request, "2", rand/31);
                bjson_finish_object(&request);
                bjson_new_object(&request, "long_index");
                bjson_append_long(&request, "0", rand/2);
                bjson_append_long(&request, "1", rand/7);
                bjson_append_long(&request, "2", rand/21);
                bjson_finish_object(&request);
                bjson_new_object(&request, "double_index");
                bjson_append_double(&request, "0", (double)rand/(double)33);
                bjson_append_double(&request, "1", (double)rand/(double)53);
                bjson_append_double(&request, "2", (double)rand/(double)73);
                bjson_finish_object(&request);
                bjson_new_object(&request, "display");
                bjson_append_string(&request, "url", url);
                sprintf(tmp, "http://demo.com/%d.jpg", k);
                bjson_append_string(&request, "img", tmp);
                bjson_finish_object(&request);
                bjson_finish(&request);
                if(isout)
                {
                    record.data = request.data;
                    record.ndata = request.current;
                    bjson_json(&record, tmp);
                    fprintf(stdout, "json:%s\n", tmp);
                }
                if((ret = dbase_set_record(&db, key, &request)) < 0)
                {
                    fprintf(stderr, "set_data(%lld) ndata:%d failed, ret:%d\n", (long long)key, request.current, ret);
                    _exit(-1);
                }
                //_exit(-1);
                list[x++] = key;
            }
            if(isout)
            {
                TIMER_SAMPLE(timer);
                fprintf(stdout, "db-time-used:%lld\n", PT_LU_USEC(timer));
                TIMER_SAMPLE(timer);
            }
            if((packetid = mtask_push(&mtask, flag, block, sizeof(int64_t) * XX_PACKET_MAX)) >= 0)
            {
                if(isout)
                {
                    TIMER_SAMPLE(timer);
                    fprintf(stdout, "mtask-time-used:%lld\n", PT_LU_USEC(timer));
                    fprintf(stdout, "1:{%d:{packetid:%d}}\n", i, packetid);
                }
                ++i;
            }
            else 
            {
                fprintf(stderr, "push mtask failed, %s\n", strerror(errno));
                sleep(1);
            }
        }while(k < to);
        bjson_clean(&request);
        dbase_close(&db);
        mtask_close(&mtask);
        TIMER_CLEAN(timer);
    }
    return 0;
}
Exemple #15
0
//gcc -o dmap dmap.c -DDMAP_TEST -DTEST_INS -DHAVE_PTHREAD -lpthread && ./dmap
int main()
{
    DMAP *dmap = NULL;
    int i = 0, j = 0, n = 0, total = 0, no = 0, stat[MASK], stat2[MASK];
    double val = 0, from = 0, to = 0, *res = NULL;
    double inputs[256], nos[256], last[256];
    double all = 0;
    time_t stime = 0, etime = 0;
    void *timer = NULL;

    if((dmap = dmap_init("/tmp/1.idx")))
    {
        res = (double *)calloc(60000000, sizeof(double));
        TIMER_INIT(timer);
#ifdef TEST_INS
        //fprintf(stdout, "sizeof(stat):%d\n", sizeof(stat));
        memset(stat, 0, sizeof(stat));
        memset(stat2, 0, sizeof(stat2));
        srand(time(NULL));
        n = 256;
        for(i = 0; i < n; i++)
        {
            no = (rand()%MASK);
            nos[i] = no;
            if((i % 3) == 0)
                inputs[i] = no * -1;
            else
                inputs[i] = no;
        }
        TIMER_RESET(timer);
        for(i = 1; i < 20000000; i++)
        {
           j = (rand()%n);
           val = inputs[j];
           no = nos[j];
           stat[no]++;
           dmap_set(dmap, i, val);
           last[j] = i;
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "set() 40000000 data, time used:%lld\n", PT_LU_USEC(timer));
        TIMER_RESET(timer);
        for(i = 0; i < n; i++)
        {
            dmap_del(dmap, last[i]);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "del() time used:%lld\n", PT_LU_USEC(timer));
        TIMER_RESET(timer);
        for(i = 0; i < n; i++)
        {
            val = inputs[i];
            no = nos[i];
            stat2[no] = dmap_in(dmap, val, res);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "in() time used:%lld\n", PT_LU_USEC(timer));
        TIMER_RESET(timer);
        total = dmap_ins(dmap, inputs, n, NULL);
        TIMER_SAMPLE(timer);
        fprintf(stdout, "ins(keys, NULL) total:%d time used:%lld\n", total, PT_LU_USEC(timer));
        TIMER_RESET(timer);
        total = dmap_ins(dmap, inputs, n, res);
        TIMER_SAMPLE(timer);
        fprintf(stdout, "ins(keys, res:%p) total:%d time used:%lld\n", res, total, PT_LU_USEC(timer));
        for(i = 0; i < n; i++)
        {
            j = nos[i];
            if(stat[j] != stat2[j])
                fprintf(stdout, "%d:%d/%d::%d\n", j, stat[j], stat2[j], inputs[i]);
        }
#ifdef OUT_ALL
        for(i = 0; i < total; i++)
        {
            fprintf(stdout, "%d:%d\n", i, res[i]);
        }
#endif
#endif
        /*
        for(i = 0; i < dmap->state->count; i++)
        {
            fprintf(stdout, "%d:{min:%d max:%d}(%d)\n", i, dmap->slots[i].min, dmap->slots[i].max, dmap->slots[i].count);
        }
        */
#ifdef TEST_RANGEFILTER
            dmap_set(dmap, 1, 1234567);
            dmap_set(dmap, 2, 1567890);
            fprintf(stdout, "rangefrom():%d\n", dmap_rangefrom(dmap, 1569000, NULL));
            fprintf(stdout, "rangeto():%d\n", dmap_rangeto(dmap, 1111111, NULL));
            fprintf(stdout, "range():%d\n", dmap_range(dmap, 1111111, 1600000, NULL));
#endif
#ifdef TEST_RANGE
        srand(time(NULL));
        TIMER_RESET(timer);
        for(i = 1; i < 40000000; i++)
        {
            val = 1356969600 + (rand()%31536000);
            dmap_set(dmap, i, val);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "set() 40000000 timestamps,  time used:%lld\n", PT_LU_USEC(timer));
        fflush(stdout);
        srand(time(NULL));
        TIMER_RESET(timer);
        all = 0;
        for(i = 0; i < 1000; i++)
        {
            val = 1356969600 + (rand()%31536000);
            all += dmap_rangefrom(dmap, val, res);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "rangefrom() 1000 times total:%lld, time used:%lld\n", (long long int)all, PT_LU_USEC(timer));
        fflush(stdout);
        srand(time(NULL));
        TIMER_RESET(timer);
        all = 0;
        for(i = 0; i < 1000; i++)
        {
            val = 1356969600 + (rand()%31536000);
            all += dmap_rangeto(dmap, val, res);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "rangeto() 1000 times total:%lld, time used:%lld\n", (long long int)all, PT_LU_USEC(timer));
        fflush(stdout);
        srand(time(NULL));
        TIMER_RESET(timer);
        all = 0;
        for(i = 0; i < 1000; i++)
        {
            from = 1356969600 + (rand()%31536000);
            to = from + rand()%31536000;
            all += dmap_range(dmap, from, to, res);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "range(%p) 1000 times total:%lld, time used:%lld\n", res, (long long int)all, PT_LU_USEC(timer));
        fflush(stdout);
        srand(time(NULL));
        TIMER_RESET(timer);
        all = 0;
        for(i = 0; i < 1000; i++)
        {
            from = 1356969600 + (rand()%31536000);
            to = from + rand()%31536000;
            all += dmap_range(dmap, from, to, NULL);
        }
        TIMER_SAMPLE(timer);
        fprintf(stdout, "range(null) 1000 times total:%lld, time used:%lld\n", (long long int)all, PT_LU_USEC(timer));
        fflush(stdout);

#endif
        dmap_close(dmap);
        TIMER_CLEAN(timer);
        free(res);
    }
}
Exemple #16
0
int new_request()
{
    int fd = 0, flag = 0, n = 0, opt = 1, prot = 0;
    struct sockaddr_in  lsa;
    socklen_t lsa_len = sizeof(struct sockaddr);

    if(ncompleted > 0 && ncompleted%1000 == 0)
    {
        TIMER_SAMPLE(timer);
        fprintf(stdout, "request:%d completed:%d time:%lld avg:%lld\n", nrequest, ncompleted, PT_USEC_U(timer), (long long int)ncompleted * 1000000ll/PT_USEC_U(timer));
    }
    if(sock_type == SOCK_DGRAM) prot = IPPROTO_UDP;
    if(nrequest < limit && (fd = socket(AF_INET, sock_type, prot)) > 0)
    {
        conns[fd].fd = fd;
        if(is_use_ssl && sock_type == SOCK_STREAM)
        {
            /* Connect */
            if(connect(fd, (struct sockaddr *)&xsa, xsa_len) != 0)
            {
                FATAL_LOG("Connect to %s:%d failed, %s", ip, port, strerror(errno));
                _exit(-1);
            }
#ifdef USE_SSL
            conns[fd].ssl = SSL_new(ctx);
            if(conns[fd].ssl == NULL )
            {
                FATAL_LOG("new SSL with created CTX failed:%s\n",
                        ERR_reason_error_string(ERR_get_error()));
                _exit(-1);
            }
            if(SSL_set_fd(conns[fd].ssl, fd) == 0)
            {
                FATAL_LOG("add SSL to tcp socket failed:%s\n",
                        ERR_reason_error_string(ERR_get_error()));
                _exit(-1);
            }
            /* SSL Connect */
            if(SSL_connect(conns[fd].ssl) < 0)
            {
                FATAL_LOG("SSL connection failed:%s\n",
                        ERR_reason_error_string(ERR_get_error()));
                _exit(-1);
            }
#endif
        }
        /* set FD NON-BLOCK */
        if(sock_type == SOCK_STREAM)
        {
           flag = fcntl(fd, F_GETFL, 0)|O_NONBLOCK;
            fcntl(fd, F_SETFL, flag);
            if(!is_use_ssl)
            {
                /* Connect */
                if(connect(fd, (struct sockaddr *)&xsa, xsa_len) != 0 && errno != EINPROGRESS)
                {
                    FATAL_LOG("Connect to %s:%d failed, %s", ip, port, strerror(errno));
                    _exit(-1);
                }
            }
            event_set(&conns[fd].event, fd, E_READ|E_WRITE|E_PERSIST, 
                    (void *)&(conns[fd].event), &ev_handler);
        }
        else
        {
            memset(&lsa, 0, sizeof(struct sockaddr));
            lsa.sin_family = AF_INET;
            if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 
                        (char *)&opt, (socklen_t) sizeof(int)) != 0
#ifdef SO_REUSEPORT
                    || setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, 
                        (char *)&opt, (socklen_t) sizeof(int)) != 0
#endif
                    || bind(fd, (struct sockaddr *)&lsa, sizeof(struct sockaddr)) != 0) 
            {
                FATAL_LOG("Bind %d to %s:%d failed, %s",
                        fd, inet_ntoa(lsa.sin_addr), ntohs(lsa.sin_port), strerror(errno));
                close(fd);
                return -1;
            }
            //while(1)sleep(1);
            /* Connect */
            /*
            if(connect(fd, (struct sockaddr *)&xsa, xsa_len) != 0)
            {
                FATAL_LOG("Connect to %s:%d failed, %s", ip, port, strerror(errno));
                _exit(-1);
            }
            */
            getsockname(fd, (struct sockaddr *)&lsa, &lsa_len);
            SHOW_LOG("Connected to remote[%s:%d] local[%s:%d] via %d", ip, port, inet_ntoa(lsa.sin_addr), ntohs(lsa.sin_port), fd);
            n = atoi(ip);
            if(n >= 224 && n <= 239)
            {
                struct ip_mreq mreq;
                memset(&mreq, 0, sizeof(struct ip_mreq));
                mreq.imr_multiaddr.s_addr = inet_addr(ip);
                mreq.imr_interface.s_addr = INADDR_ANY;
                if(setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,(char*)&mreq, sizeof(mreq)) != 0)
                {
                    SHOW_LOG("Setsockopt(MULTICAST) failed, %s", strerror(errno));
                    return -1;
                }
                if(setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &lsa.sin_addr, sizeof(struct in_addr)) < 0)
                {
                    FATAL_LOG("Setsockopt(IP_MULTICAST_IF) failed, %s", strerror(errno));
                    return -1;
                }
            }
            event_set(&conns[fd].event, fd, E_READ|E_WRITE|E_PERSIST, 
                    (void *)&(conns[fd].event), &ev_udp_handler);
        }
        evbase->add(evbase, &(conns[fd].event));
        conns[fd].nresp = 0;
        if(keepalive)
            conns[fd].nreq = sprintf(conns[fd].request, "GET / HTTP/1.0\r\nConnection: Keep-Alive\r\n\r\n");
        else
            conns[fd].nreq = sprintf(conns[fd].request, "GET / HTTP/1.0\r\n\r\n");
    }
    else
    {
        if(ncompleted >= limit) running_status = 0;
    }
    return 0;
}