long int without_virtual_time()
{
    struct timeval prev;
    struct timeval next;
    struct timeval diff;
    struct timeval tmp;

    long ret;
    ret = gettimeofday(&prev, NULL);
    chk_sys_call_ret(ret, "gettimeofday");

    long int i;
    for (i = 0; i < NR_ROUND; ++i)
    {
        ret = gettimeofday(&tmp, NULL);
        chk_sys_call_ret(ret, "gettimeofday");
    }
    ret = gettimeofday(&next, NULL);
    chk_sys_call_ret(ret, "gettimeofday");
    ret = timeval_substract(&diff, &next, &prev);
    printf("Elapsed %ld seconds, %ld useconds\n", diff.tv_sec, diff.tv_usec);

    long int usec = timeval_to_usec(diff);
    printf("Elapsed %ld useconds without virtual time\n", usec);
    return usec;
}
Exemple #2
0
/**
 * Wait at least the specified number of milliseconds. This function is used
 * to limit the number of connections per second.
 */
void delay_connection(struct sslCheckOptions *options)
{
	struct timeval next;
	struct timeval result;
	struct timeval cur_time;
	struct timespec delay;

	if (options->connection_delay <= 0)
		return;

	next = options->connection_time;
	next.tv_sec += options->connection_delay / 1000;
	next.tv_usec += options->connection_delay % 1000 * 1000;

	while (next.tv_usec > 999999) {
		next.tv_sec++;
		next.tv_usec -= 1000000;
	}

	gettimeofday(&cur_time, NULL);

	while(timeval_substract(&next, &cur_time, &result) >= 0) {
		delay.tv_sec = result.tv_sec;
		delay.tv_nsec = result.tv_usec * 1000;
		nanosleep(&delay, NULL);
		gettimeofday(&cur_time, NULL);
	}
	options->connection_time = cur_time;
}
/**
 * more straightforward way is:
 * char cmd[100];
 * sprintf(cmd, "echo %d > /proc/%d/dilation", tdf, pid);
 * system(cmd);
 */
int set_new_dilation(pid_t pid, float tdf)
{
        char tdf_str[TDF_STR_LEN];
        size_t count;
        ssize_t written_count = 0;
#ifdef SHOW_OVHD
        struct timeval prev, next, ovhd;
        long int usec;

        gettimeofday(&prev, NULL);
#endif
        if (tdf >= TDF_MIN && tdf < TDF_MAX){
                /* echo 1000*TDF to kernel */
                count = sprintf(tdf_str, "%d", (int)(tdf * 1000));
                /* count = strlen(tdf_str) */
                written_count = write_proc_field(pid, "dilation", tdf_str); //write(proc_file, tdf_str, count); 
        }
#ifdef SHOW_OVHD
        gettimeofday(&next, NULL);
        timeval_substract(&ovhd, &next, &prev);
        usec = timeval_to_usec(ovhd);
        printf("[set dilation ovhd = %ld]\n", usec);
#endif
        return written_count;
}
Exemple #4
0
void wacom_callback(int i) {	
	gettimeofday(&tv,NULL);
	timeval_substract(&tvdiff,&tv,&tv0);
	fprintf(data,"\t %d \t %d \t %d \t %d \t %ld \t %d \n", i,
			(int)(gAbsState[WACOMFIELD_POSITION_X].nValue * X_FACTOR),
			(int)(gAbsState[WACOMFIELD_POSITION_Y].nValue * Y_FACTOR),
			(int)(gAbsState[WACOMFIELD_PRESSURE].nValue),
			tvdiff.tv_sec, (int)(tvdiff.tv_usec / 1000));
	//fflush(stdout);
}
long int with_virtual_time()
{
    struct timeval prev;
    struct timeval next;
    struct timeval diff;
    struct timeval tmp;

    long ret;
    int status;
    int pid = fork();

    ret = gettimeofday(&prev, NULL);
    chk_sys_call_ret(ret, "gettimeofday");

    if (pid == -1)
    {
        printf("\n[error] clone_time fails with error: %s\n", strerror(errno));

    } else if (pid == 0) {

        ret = virtualtimeunshare(CLONE_NEWNET|CLONE_NEWNS, 4);
        chk_sys_call_ret(ret, "virtualtimeunshare");

        long int i;
        for (i = 0; i < NR_ROUND; ++i)
        {
            ret = gettimeofday(&tmp, NULL);
            chk_sys_call_ret(ret, "gettimeofday");
        }
        exit(EXIT_SUCCESS);
        
    } else {

        pid = wait(&status);
        // if (status == -1)
        // {
        //     perror("wait error");
        //     return -1;
        // }
        // if (WIFEXITED(status) != 0) {
        //     printf("Child process ended normally; status = %d\n", WEXITSTATUS(status));
        // }
        ret = gettimeofday(&next, NULL);
        chk_sys_call_ret(ret, "gettimeofday");
        ret = timeval_substract(&diff, &next, &prev);
        printf("Elapsed %ld seconds, %ld useconds\n", diff.tv_sec, diff.tv_usec);

        long int usec = timeval_to_usec(diff);
        printf("Elapsed %ld useconds with virtual time\n", usec);
        return usec;
    }
}
Exemple #6
0
GSList *getugroups(char *username, gid_t gid)
{
	GSList *grouplist = NULL;
	int i, ng = 0;
	gid_t *groups = NULL;

	/* need to lock as position is common to all thread */
	g_static_mutex_lock(&group_mutex);

#ifdef PERF_DISPLAY_ENABLE
	{
		struct timeval tvstart, tvend, result;
		if (nuauthconf->debug_areas & DEBUG_AREA_PERF) {
			gettimeofday(&tvstart, NULL);
		}
#endif
		if (system_glibc_cant_guess_maxgroups) {
			ng = system_glibc_cant_guess_maxgroups;
		} else {
			if (getgrouplist(username, gid, NULL, &ng) >= 0) {
				return NULL;
			}
		}

		groups = g_new0(gid_t, ng);
		getgrouplist(username, gid, groups, &ng);

		for (i = 0; i < ng; i++) {
			grouplist =
			    g_slist_prepend(grouplist,
					    GINT_TO_POINTER(groups[i]));
		}

		g_free(groups);

#ifdef PERF_DISPLAY_ENABLE
		if (nuauthconf->debug_areas & DEBUG_AREA_PERF) {
			gettimeofday(&tvend, NULL);
			timeval_substract(&result, &tvend, &tvstart);
			log_message(INFO, DEBUG_AREA_PERF,
					"Group list fetching duration: %.1f msec",
					(double)result.tv_sec*1000+
						(double)(result.tv_usec/1000));
		}
	}
#endif

	/* release lock */
	g_static_mutex_unlock(&group_mutex);

	return grouplist;
}
void kickoff_pthreads_freeze(pid_t* pid_list, size_t size, void *(* func)(void *), char *action)
{
        pthread_t* threads;
        pthread_attr_t attr;
        int i, rc;
        void *status;
#ifdef SHOW_OVHD
        struct timeval start, end, ovhd;
        long usec;
        
        gettimeofday(&start, NULL); 
#endif
        threads = malloc(sizeof(pthread_t) * size);
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
        for (i = 0; i < size; ++i) {
                if (pthread_create(&threads[i], NULL, func, (void *)(&pid_list[i])) != 0) {
                        fprintf(stderr, "create pthreads failed\n");
                }
        }
        pthread_attr_destroy(&attr);
        for (i = 0; i < size; ++i) {
                rc = pthread_join(threads[i], &status);
                if (rc) {
                        fprintf(stderr, "join pthread fail with %d\n", rc);
                        exit(-1);
                }
                /*printf("%s thread[%d] joined for pid[%d]\n", action, i, *((pid_t*)status));*/
        }
#ifdef SHOW_OVHD        
        gettimeofday(&end, NULL);
        timeval_substract(&ovhd, &end, &start);
        usec = timeval_to_usec(ovhd);
        if (strcmp(action, "freeze") == 0) {
                printf("freeze %ld\n", usec);        
        } else if (strcmp(action, "unfreeze") == 0) {
                printf("unfreeze %ld\n", usec);         
        } else {
                printf("unknown action %s\n", action);  
        }
#endif
        /* should issue exit on main thread if we do join() */
        /*pthread_exit(NULL);*/
}
int unfreeze_proc(pid_t pid)
{
        char *val = "0";
#ifdef SHOW_OVHD
        struct timeval prev, next, ovhd;
        long int usec;
        int result;

        gettimeofday(&prev, NULL);
        result = write_proc_field(pid, "freeze", val); //write_proc_freeze(pid, val);
        gettimeofday(&next, NULL);
        timeval_substract(&ovhd, &next, &prev);
        usec = timeval_to_usec(ovhd);
        printf("[unfreeze proc %d] takes %ld]\n", pid, usec);

        return result;
#else
        return write_proc_field(pid, "freeze", val); //write_proc_freeze(pid, val);
#endif
}
static void handle_ack_pkt(uint8_t *data, size_t len)
{
    // ACK_FRAME | config_type | [config]
    (void)len;
    uint8_t ack_type = data[1];
    uint8_t config   = data[2];
    struct timeval time_ack, time_diff;


    switch (ack_type) {
        case SET_TIME:
            gettimeofday(&time_ack, NULL);
            timeval_substract(&time_diff, &time_ack, &set_time_ref);
            PRINT_MSG("config_ack set_time %lu.%06lu\n",
                    time_diff.tv_sec, time_diff.tv_usec);
            timerclear(&set_time_ref);
            break;
        case CONFIG_CONSUMPTION:
            PRINT_MSG("config_ack config_consumption_measure\n");
            config_consumption(
                    config & (PW_SRC_3_3V | PW_SRC_5V | PW_SRC_BATT),
                    config & MEASURE_POWER,
                    config & MEASURE_VOLTAGE,
                    config & MEASURE_CURRENT);
            break;
        case CONFIG_RADIO_STOP:
            PRINT_MSG("config_ack config_radio_stop\n");
            break;
        case CONFIG_RADIO_MEAS:
            PRINT_MSG("config_ack config_radio_measure\n");
            break;
        default:
            PRINT_ERROR("Unkown ACK frame 0x%02x\n", ack_type);
            break;
    }
}
Exemple #10
0
int main ( int argc, char *argv[] )
{
    size_t nb = 0, nc, nx = 0, nbuf = 0, nsel = 0, nerr = 0, i, nh = 0, nw;
    FILE* ficin;
    FILE* ficout = NULL;
    FILE* ficol = NULL;
    unsigned char b[4], header[256];
    unsigned int expected = 0;
    char name[256], namex[256], namec[256];
    double tx;
    struct timeval tini, tfin, tt;

    // Initial time
    gettimeofday ( &tini, NULL );

    // read args from stdio
    if ( read_args ( argc, argv ) < 0 )
    {
        exit ( EXIT_FAILURE );
    }

    // Check input
    if ( stat ( ENTRADA, &INSTAT ) )
    {
        printf ( "%s: Cannot stat  %s\n", OWN, ENTRADA );
        exit ( EXIT_FAILURE );
    }

    // Open output file
    if ( ( ficin = fopen ( ENTRADA,"r" ) ) == NULL )
    {
        printf ( "%s: Cannot open %s\n", OWN, ENTRADA );
        exit ( EXIT_FAILURE );
    }

    if ( COLECT )
    {
        // To make an archive

        // build a name
        strcpy ( namec, PREFIX );
        strcat ( namec, ENTRADA );

        // open the file
        if ( ( ficol = fopen ( namec, "w" ) ) == NULL )
        {
            printf ( "%s: Cannot open %s\n", OWN, namec );
            exit ( EXIT_FAILURE );
        }
    }


    STAGE = 0;

    memset ( &b, 0, 4 * sizeof ( unsigned char ) );

    while ( ( nc = fread ( &BUF[0], sizeof ( unsigned char ), BLEN, ficin ) ) > 0 )
    {
        for ( i = 0; i < nc ; i++ )
        {
            // ingest a byte
            b[3] = b[2];
            b[2] = b[1];
            b[1] = b[0];
            b[0] = BUF[i];

            switch ( STAGE )
            {
            case 0: // begining, searching header init
                if ( is_head ( &b[0] ) )
                {
                    memset ( &header[0], 0, 64 * sizeof ( unsigned char ) );
                    header[0] = '*';
                    header[1] = '*';
                    header[2] = '*';
                    header[3] = '*';
                    nh = 4;
                    STAGE = 1;
                }
                break;
            case 1: // header found, filling
                if ( nh < 255 )
                    header[nh++] = b[0];
                else
                {
                    STAGE = 0;
                    nerr++;
                    break;
                }
                if ( is_head ( &b[0] ) )
                {
                    STAGE = 2;
                    //header[nh++] = '\0';
                    //printf("Header: '%s'\n",header);
                }
                break;
            case 2: // header already finished
                if ( nh < 255 )
                    header[nh++] = b[0];
                else
                {
                    STAGE = 0;
                    nerr++;
                    break;
                }
                if ( b[0] != 0x0a && b[0] != 0x0d )
                {
                    STAGE = 3;
                    memset ( &name[0], 0, 128 * sizeof ( unsigned char ) );
                    name[0] = b[0];
                    nx = 1;
                }
                break;
            case 3: // name already inited
                if ( nh < 255 )
                    header[nh++] = b[0];
                else
                {
                    STAGE = 0;
                    nerr++;
                    break;
                }
                if ( nx >= 255 )
                {
                    STAGE = 0;
                    break;
                }
                if ( b[0] == 0x01a || b[0] == 0x0d )
                {
                    STAGE = 4;
                    name[nx++] = '\0';
                    //printf("Name: '%s'\n",name);
                }
                else if ( b[0] == ' ' )
                    name[nx++] = '_'; // substitute a space by '_'
                else
                    name[nx++] = b[0];
                break;
            case 4: // Waiting BUFR message begin
                if ( nh < 255 )
                    header[nh++] = b[0];
                else
                {
                    STAGE = 0;
                    nerr++;
                    break;
                }
                if ( is_bufr ( &b[0] ) )
                {
                    STAGE = 5;
                    expected = 0;
                    // printf("Leyendo BUFR\n");
                    memset ( &BUFR[0], 0, BUFRLEN );
                    nb = 4;
                    BUFR[0]='B';
                    BUFR[1]='U';
                    BUFR[2]='F';
                    BUFR[3]='R';
                    nh -= 4;
                    header[nh] = '\0';
                }
                break;
            case 5: // Filling BUFR message till final '7777'
                if ( nb < ( BUFRLEN - 1 ) )
                    BUFR[nb++] = b[0];
                else
                {
                    printf ( "Error: Bufr message length > %d", BUFRLEN );
                    fclose ( ficin );
                    exit ( EXIT_FAILURE );
                }
                // check expected length
                if (nb == 7)
                {
                    expected = (unsigned int)b[0] + (unsigned int)b[1] * 256 + (unsigned int)b[2] * 65536;
                }

                // Has been detected some void and fakes bufr
                if ( b[0] == '*' &&
                        b[1] == '*' &&
                        b[2] == '*'
                   )
                {
                    // Ooops. a fake bufr
                    // it seems a new header has been found before '7777'
                    STAGE = 0;
                    nerr++;
                    break;
                }
                if ( nb == expected)
                {
                    if ( is_endb ( &b[0] ) )
                    {
                        STAGE = 0;
                        nbuf++;
                        if ( LISTF )
                            printf ( "%s\n", name );
                        if ( bufr_is_selected ( name ) )
                        {
                            nsel++;
                            if ( INDIVIDUAL )
                            {
                                // prefix with input file timestamp
                                date_mtime_from_stat ( namex, &INSTAT );
                                strcat ( namex,"_" );
                                strcat ( namex, name );
                                strcat ( namex, ".bufr" );
                                if ( ( ficout = fopen ( namex, "w" ) ) == NULL )
                                {
                                    printf ( "Error: cannot open %s\n", name );
                                    fclose ( ficin );
                                    exit ( EXIT_FAILURE );
                                }
                                if ( ( nw = fwrite ( &BUFR[0], sizeof ( unsigned char ), nb, ficout ) ) != nb )
                                {
                                    printf ( "Error: Writen %lu bytes instead of %lu in %s file\n", nw, nb, namex );
                                    fclose ( ficin );
                                    fclose ( ficout );
                                    exit ( EXIT_FAILURE );
                                }
                                // close an individual fileq
                                fclose ( ficout );
                                // change individual file timestamp
                                mtime_from_stat ( namex, &INSTAT );
                            }
                            if ( COLECT )
                            {
                                // first write header
                                if ( ( nw = fwrite ( &header[0], sizeof ( char ), nh, ficol ) ) != nh )
                                {
                                    printf ( "%s: Error: Writen %lu bytes instead of %lu in %s file\n", OWN, nw, nh, namec );
                                    fclose ( ficin );
                                    fclose ( ficol );
                                    exit ( EXIT_FAILURE );
                                }

                                // then bufr message
                                if ( ( nw = fwrite ( &BUFR[0], sizeof ( unsigned char ), nb, ficol ) ) != nb )
                                {
                                    printf ( "%s: Error: Writen %lu bytes instead of %lu in %s file\n", OWN, nw, nb, namex );
                                    fclose ( ficin );
                                    fclose ( ficout );
                                    exit ( EXIT_FAILURE );
                                }
                                // finally \r\r\n
                                if ( ( nw = fwrite ( &SEP[0], sizeof ( char ), 3, ficol ) ) != 3 )
                                {
                                    printf ( "%s: Error: Writen %lu bytes instead of 3 chars separing messages in %s\n", OWN, nw, namex );
                                    fclose ( ficin );
                                    fclose ( ficout );
                                    exit ( EXIT_FAILURE );
                                }
                            }
                        }
                    }
                    else
                    {   // reached the expected end of BUFR without a '7777'
                        STAGE = 0;
                        nerr++;
                        break;
                    }
                }
            }
        }
    }

    if ( COLECT )
    {
        fclose ( ficol );
        // change archive file timestamp
        mtime_from_stat ( namec, &INSTAT );
    }

    // Final time
    gettimeofday ( &tfin, NULL );
    fclose ( ficin );

    // A brief stat output
    if ( VERBOSE )
    {
        printf ( "Found %lu bufr reports. Selected: %lu. Wrong: %lu\n", nbuf, nsel, nerr );
        timeval_substract ( &tt, &tfin, &tini );
        tx = ( double ) tt.tv_sec + ( double ) tt.tv_usec *1e-6;
        printf ( "%lf seg.  ", tx );
        if ( nbuf && tx != 0.0 )
            printf ( "%lf reports/sec.\n", ( double ) nbuf / tx );
        else
            printf ( "\n" );
    }
    exit ( EXIT_SUCCESS );
}
Exemple #11
0
/**
 * Ask each client of global_msg address set to send their new connections
 * (connections in stage "SYN SENT").
 *
 * \param global_msg Address set of clients
 * \return Returns 0 on error, 1 otherwise
 */
char warn_clients(struct msg_addr_set *global_msg,
		  user_session_check_t *scheck,
		  gpointer data)
{
	ip_sessions_t *ipsessions = NULL;
	GSList *ipsockets = NULL;
	struct timeval timestamp;
	struct timeval interval;
#if DEBUG_ENABLE
	if (DEBUG_OR_NOT(DEBUG_LEVEL_VERBOSE_DEBUG, DEBUG_AREA_USER)) {
		char addr_ascii[INET6_ADDRSTRLEN];
		format_ipv6(&global_msg->addr, addr_ascii, INET6_ADDRSTRLEN, NULL);
		g_message("Warn client(s) on IP %s", addr_ascii);
	}
#endif

	lock_client_datas();
	ipsessions = g_hash_table_lookup(client_ip_hash, &global_msg->addr);
	if (ipsessions) {
		global_msg->found = TRUE;
		/* if data or scheck is not NULL we need to send the message and thus
		we do not enter the delay code */
		if ((!(data || scheck)) && ipsessions->proto_version >= PROTO_VERSION_V22_1) {
			gettimeofday(&timestamp, NULL);
			timeval_substract(&interval, &timestamp, &(ipsessions->last_message));
			if ((interval.tv_sec == 0) && ((unsigned)interval.tv_usec < nuauthconf->push_delay)) {
				unlock_client_datas();
				return 1;
			} else {
				ipsessions->last_message.tv_sec = timestamp.tv_sec;
				ipsessions->last_message.tv_usec = timestamp.tv_usec;
			}
		}
		for (ipsockets = ipsessions->sessions; ipsockets; ipsockets = ipsockets->next) {
			user_session_t *session = (user_session_t *)ipsockets->data;

			if ((!scheck) || scheck(session, data)) {
				struct msg_addr_set *gmsg = g_memdup(global_msg, sizeof(*global_msg));
				gmsg->msg = g_memdup(global_msg->msg,
						     ntohs(global_msg->msg->length));
#if DEBUG_ENABLE
				if (DEBUG_OR_NOT(DEBUG_LEVEL_VERBOSE_DEBUG, DEBUG_AREA_USER)) {
					char addr_ascii[INET6_ADDRSTRLEN];
					format_ipv6(&global_msg->addr, addr_ascii, INET6_ADDRSTRLEN, NULL);
					g_message("Queuing message for %s (%d)",
						  addr_ascii,
						  session->socket);
				}
#endif
				g_async_queue_push(session->workunits_queue, gmsg);
				if (session->activated) {
					session->activated = FALSE;
					g_async_queue_push(writer_queue,
							   GINT_TO_POINTER(session->socket));
					ev_async_send(session->srv_context->loop,
							&session->srv_context->client_writer_signal);
				}
			}
		}

		unlock_client_datas();
		return 1;
	} else {
		global_msg->found = FALSE;
		unlock_client_datas();
		return 0;
	}
}