Example #1
0
File: cpu.c Project: satai/firejail
void read_cpu_list(const char *str) {
	char *tmp = strdup(str);
	if (tmp == NULL)
		errExit("strdup");
	
	char *ptr = tmp;
	while (*ptr != '\0') {
		if (*ptr == ',' || isdigit(*ptr))
			;
		else {
			fprintf(stderr, "Error: invalid cpu list\n");
			exit(1);
		}
		ptr++;
	}
	
	char *start = tmp;
	ptr = tmp;
	while (*ptr != '\0') {
		if (*ptr == ',') {
			*ptr = '\0';
			set_cpu(start);
			start = ptr + 1;
		}
		ptr++;
	}
	set_cpu(start);
	free(tmp);
}
Example #2
0
void *player(void *p)
{
    long player_id = (long)p;
    set_cpu(player_id);

    if (config.flags & FL_MUTEX)
        if ((errno = pthread_mutex_lock(&mutex)) != 0)
            handle_error("pthread_mutex_lock player");

    /* infinite loop */
    for(;;)
    {
        wait_referee(player_id);
        player_play(player_id);
        notify_referee();

        if (hand[player_id] == STOP)
        {
            if (config.flags & FL_MUTEX)
                if ((errno = pthread_mutex_unlock(&mutex)) != 0)
                    handle_error("pthread_mutex_unlock player");
            pthread_exit(NULL);
        }
    }
}
Example #3
0
void gui_load()
{
    debug_start();

    pnd_app_get_list();
    cfg_gui_read();
    gui_load_skin();

    if ( ! ( nh = pnd_notify_init() ) )
    {
        debug_error ( "PND INOTIFY init problem spotted\n" );
    }

    if ( ! ( nh2 = pnd_dbusnotify_init() ) )
    {
        debug_error ( "PND DBUSINOTIFY init problem spotted\n" );
    }

    initStatusCalls();
    cpuUsage();
    getCPULoad();

    set_cpu( pmenu->cpu_mhz );

    debug_end();
}
Example #4
0
 void* add(void* x) 
 { 
 unsigned long long sum=0; 
 struct timeval tpstart,tpend; 
 float timeuse; 
 
 if(-1 == set_cpu(1)) 
 { 


 return NULL; 
 } 
 
 gettimeofday(&tpstart,NULL); 
 
 for(sum=0;sum<APPLE_MAX_VALUE;sum++) 
 { 
 ((struct apple *)x)->a += sum; 
 ((struct apple *)x)->b += sum; 
 } 
 
 gettimeofday(&tpend,NULL); 
 
 timeuse=MSECOND*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec; 
 timeuse/=MSECOND; 
 printf("add thread:%x,Used Time:%f\n",pthread_self(),timeuse); 
 
 return NULL; 
 } 
Example #5
0
void *prepare_and_bench_blocking(void *arg)
{
    struct dpdk_test_args *args = (struct dpdk_test_args *) arg;
    pthread_mutex_lock(&args->mutex);
    pthread_t t = pthread_self();
    set_cpu(t, args->cpu);
    args->started = true;
    pthread_cond_wait(args->cond, &args->mutex);
    auto& ctx = uhd::transport::uhd_dpdk_ctx::get();
    uhd::transport::dpdk_zero_copy::sptr eth_data[1];
    uhd::transport::zero_copy_xport_params buff_args;
    buff_args.recv_frame_size = 8000;
    buff_args.send_frame_size = 8000;
    buff_args.num_send_frames = 8;
    buff_args.num_recv_frames = 8;
    auto dev_addr = uhd::device_addr_t();
    eth_data[0] = uhd::transport::dpdk_zero_copy::make(
        ctx,
        args->portid,
        args->dst_ip,
        "48888",
        "48888",
        buff_args,
        dev_addr
    );

    bench(eth_data, 1, 0.1);
    return 0;
}
Example #6
0
void threads::worker() {
    /*run all threads on the same CPU so we don't cause the system to lag too bad*/
    set_cpu(0); /*TODO: option to set specific CPU*/

    /*initialize the indexes*/
    std::vector<size_t> block_indexes(blocks.size());
    std::vector<size_t> prng_indexes(blocks.size());
    for (size_t i = 0; i < blocks.size(); i++) {
        block_indexes[i] = i;
        prng_indexes[i] = i % prng_count;
    }


#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
    while (true) {
        /*shuffle the indexes*/
        std::shuffle(block_indexes.begin(), block_indexes.end(), primary_rng);
        std::shuffle(worker_threads.begin(), worker_threads.end(), primary_rng);

        /*generate random data in the buffer*/
        for (size_t i = 0; i < blocks.size(); i++) {
            /*TODO: put in ISAAC crypto algorithm?*/
            /*fill the block with data*/
            for (size_t j = 0; j < blocksize; j++) {
                blocks[block_indexes[i]].data[j] = prngs[prng_indexes[i]]();
            }
            /*advance the block count*/
            blocks[block_indexes[i]].count++;
        }
    }
#pragma clang diagnostic pop
}
Example #7
0
hclh_local_params* init_hclh_local(uint32_t phys_core, hclh_global_params* the_params) {
    //assign the thread to the correct core
    set_cpu(phys_core);
    hclh_local_params* local_params;
    //    local_params = (hclh_local_params**)malloc(num_locks * sizeof(hclh_local_params));
    uint32_t i;
    __sync_synchronize();
    uint32_t real_core_num = 0;
    for (i = 0; i < (NUMBER_OF_SOCKETS * CORES_PER_SOCKET); i++) {
        if (the_cores[i]==phys_core) {
            real_core_num = i;
            break;
        }
    }
    phys_core=real_core_num;
    __sync_synchronize();
    MEM_BARRIER;
    local_params=(hclh_local_params*) malloc(sizeof(hclh_local_params));
    local_params->my_qnode = (qnode*) malloc(sizeof(qnode));
    local_params->my_qnode->data = 0;
    local_params->my_qnode->fields.cluster_id  = phys_core/CORES_PER_SOCKET;
    local_params->my_qnode->fields.successor_must_wait=1;
    local_params->my_pred = NULL;
    if (phys_core%CORES_PER_SOCKET==0) {
        the_params->local_queues[phys_core/CORES_PER_SOCKET] = (local_queue*)malloc(sizeof(local_queue));
        *(the_params->local_queues[phys_core/CORES_PER_SOCKET]) = NULL;
        the_params->init_done[phys_core/CORES_PER_SOCKET]=INIT_VAL;
    }
    while(the_params->init_done[phys_core/CORES_PER_SOCKET]!=INIT_VAL) {}
    local_params->my_queue = the_params->local_queues[phys_core/CORES_PER_SOCKET];
    return local_params;
}
Example #8
0
void threads::output() {
    set_cpu(1);

    /*fill the indexes*/
    std::vector<size_t> indexes(blocks.size());
    for (size_t i = 0; i < blocks.size(); i++) { indexes[i] = i; }

    /*initialize stored last-output counts*/
    std::vector<size_t> counts(blocks.size(), 0);

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
    while (true) {

        /*mix around the indexes*/
        std::shuffle(indexes.begin(), indexes.end(), primary_rng);

        for (size_t i = 0; i < blocks.size(); i++) {

            /*if the block is new*/
            if (blocks[indexes[i]].count > counts[indexes[i]]) {

                /*update the block count*/
                counts[indexes[i]] = blocks[indexes[i]].count;
                output_stream.write((const char *) blocks[indexes[i]].data,
                                    blocksize);
                output_stream.flush();

            }
        }
    }
#pragma clang diagnostic pop
}
Example #9
0
void *referee()
{
    set_cpu(0);

    if (config.flags & FL_MUTEX)
        if ((errno = pthread_mutex_lock(&mutex)) != 0)
            handle_error("pthread_mutex_lock referee");

    /* infinite loop */
    for (;;)
    {
        if ((rand_r(&seed)) < config.referee_end_prob)
        {
            prtflu("REFEREE: game is over\n");
            if (config.flags & FL_MUTEX)
                if ((errno = pthread_mutex_unlock(&mutex)) != 0)
                    handle_error("pthread_mutex_unlock referee");
            pthread_exit(NULL);
        }
        else
        {
            hand[0] = hand[1] = NONE;
            prtflu("REFEREE: game is starting...\n");
            notify_players();
            wait_players();
            evaluate_player();
            game_response();
        }
        wait_key();
    }
}
Example #10
0
/*
 * Actually perform cpuid operation.
 */
static int
cpuctl_do_cpuid_count(int cpu, cpuctl_cpuid_count_args_t *data,
    struct thread *td)
{
	int is_bound = 0;
	int oldcpu;

	KASSERT(cpu >= 0 && cpu <= mp_maxid,
	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));

	/* Explicitly clear cpuid data to avoid returning stale info. */
	bzero(data->data, sizeof(data->data));
	DPRINTF("[cpuctl,%d]: retrieving cpuid lev %#0x type %#0x for %d cpu\n",
	    __LINE__, data->level, data->level_type, cpu);
#ifdef __i386__
	if (cpu_id == 0)
		return (ENODEV);
#endif
	oldcpu = td->td_oncpu;
	is_bound = cpu_sched_is_bound(td);
	set_cpu(cpu, td);
	cpuid_count(data->level, data->level_type, data->data);
	restore_cpu(oldcpu, is_bound, td);
	return (0);
}
Example #11
0
int main(int argc, char *argv[])
{
	// taking options : number of task
	//int ntask = 100;
	int pid_d, pid_c1, pid_c2, status;
	char buffer[10];

	// Creates consumers & dispatcher

	pid_d = fork();
	if( pid_d == 0 ){

		// Becomes dispatcher

		execlp("./dispatcher",
				"dispatcher", (char*)NULL);
		printf("Exec fallita!\n");
		exit(1);
	}
	else{
		pid_c1 = fork();
		if( pid_c1 == 0 ){

			// Becomes consumer 1

			execlp("./consumer1", "consumer1",
					buffer, "/tmp/myfifo1", (char*)NULL);
			printf("Exec fallita!\n");
			exit(1);
		}
		else{
			pid_c2 = fork();
			if( pid_c2 == 0 ){

				// Becomes consumer2

				execlp("./consumer2", "consumer2",
						buffer, "/tmp/myfifo2", (char*)NULL);
				printf("Exec fallita!\n");
				exit(1);
			}
			else{

				// Setting affinity

				set_cpu(pid_d, pid_c1, pid_c2);

				// Waits children

				wait(&status);
			}

		}

	}

	return 0;

}
Example #12
0
void disturb( void ){

	set_cpu(CPU_USED);
	int someVar = 0;
	while(1){
		someVar++;
	}
}
Example #13
0
// Performs a test of the pid_table module:
int main(int argc, char *argv[])
{
	int n, pid, cpu, new_cpu;
	pid_t pids[num_entry];
	pid_table table;

	// Create table:
	printf("creating pid_table index size: %i buckets: %i\n", index_size, bucket_count);
	table = create_pid_table(index_size, bucket_count);
	if (table == NULL)
	{
		printf("failed!\n");
		return 1;
	}
	printf("OK!\n");
	// Seed rand():
	srand(time(NULL ));
	// Add entries:
	printf("adding %i entries\n", num_entry);
	for (n = 0; n < num_entry; n++)
	{
		pid = rand();
		pids[n] = pid;
		cpu = rand() % num_cpu;
		if (add_pid_to_pid_table(table, pid, cpu) != 0)
		{
			printf("failed!\n");
			return 1;
		}
	}
	printf("OK!\n");
	printf("switching all cpus\n");
	for (n = 0; n < num_entry; n++)
	{
		new_cpu = rand() % 64;
		if (set_cpu(table, pids[n], new_cpu) != 0)
		{
			printf("failed!\n");
			return 1;
		}
	}
	printf("OK!\n");
	// Remove elements:
	printf("removing each entry\n");
	for (n = 0; n < num_entry; n++)
	{
		if (remove_pid_from_pid_table(table, pids[n]) != 0)
		{
			printf("failed!\n");
			return 1;
		}
	}
	printf("OK!\n");
	printf("destroying table\n");
	destroy_pid_table(table);
	printf("OK!\n");
	return 0;
}
Example #14
0
File: mcs.c Project: respu/libslock
mcs_qnode* init_mcs_local(uint32_t thread_num) {
    set_cpu(thread_num);

    mcs_qnode*  the_qnodes=(mcs_qnode*)malloc(sizeof(mcs_qnode));

    MEM_BARRIER;
    return the_qnodes;

}
Example #15
0
int init_spinlock_local(uint32_t thread_num, uint32_t* limit)
{
    //assign the thread to the correct core
    set_cpu(thread_num);
    *limit = 1;
    spinlock_seeds = seed_rand();
    MEM_BARRIER;
    return 0;
}
Example #16
0
int init_mcs_local(uint32_t thread_num, mcs_qnode** the_qnode) {
    set_cpu(thread_num);

    (*the_qnode)=(mcs_qnode*)malloc(sizeof(mcs_qnode));

    MEM_BARRIER;
    return 0;

}
Example #17
0
void *test(void *data) {
	fprintf(stderr, "Starting test\n");
	//get the per-thread data
    thread_data_t *d = (thread_data_t *)data;

    //place the thread on the apropriate cpu
    set_cpu(the_cores[d->id]);
    int op_count = 10000;

    ssalloc_init();
    bst_init_local(d->id);

    /* Wait on barrier */
    barrier_cross(d->barrier);

	int i;
	bool_t added;

	for ( i = 1; i <= op_count; i++){

		added = bst_insert(i, root, d->id);
		// fprintf(stderr, "[%d] Added %d? %d\n", d->id, i, added==TRUE);
		if (added == TRUE) {
			d->num_insert++;
		}
	}

	// printf("Root right node: %d", root->right->key);
	
	for ( i = 1; i <= op_count; i++){

		node_t* found = bst_find(i, root, d->id);
		// printf("Contains %d? %d\n", i, found==FOUND);
		if (found != NULL) {
			d->num_search ++;
		} 
	}

	for ( i = 1; i <= op_count; i++){

		bool_t removed = bst_delete(i, root, d->id);
		// printf("Removed %d? %d\n", i, removed==TRUE);
		if (removed == TRUE) {
			d->num_remove ++;
		}
	}

	// for ( i = 1; i < 10; i++){

	// 	bool_t found = bst_contains(i);
	// 	printf("Contains %d? %d\n", i, found==FOUND);
	// }


	return NULL;
}
Example #18
0
array_lock_t* init_alock_local(uint32_t thread_num, lock_shared_t* the_lock) {
    //assign the thread to the correct core
    set_cpu(thread_num);

    uint32_t i;
    array_lock_t* local_lock = (array_lock_t*) malloc(sizeof(array_lock_t));
    local_lock->my_index=0;
    local_lock->shared_data = the_lock;
    return local_lock;
}
Example #19
0
void* disturbance() {
	set_cpu(CPU_CORE);
	double i, j, k;
	while(1) {
		i++;
		j++;
		k = i*j;
		k++;
	}
}
Example #20
0
uint32_t* init_rw_ttas_array_local(uint32_t thread_num, uint32_t size){
    set_cpu(thread_num);
    rw_seeds = seed_rand();
    uint32_t* limits;
    limits = (uint32_t*)malloc(size * sizeof(uint32_t));
    uint32_t i;
    for (i = 0; i < size; i++) {
        limits[i]=1;
    }
    return limits;
}
Example #21
0
void* test(void* num_param) {
	int num = (long) num_param;
	set_cpu(CPU_CORE);
	io_write(num, 1);
	while(1) {
		while (io_read(num));

		io_write(num, 0);
		usleep(5);
		io_write(num, 1);
	}
}
Example #22
0
uint32_t* init_ttas_array_local(uint32_t thread_num, uint32_t size){
    //assign the thread to the correct core
    set_cpu(thread_num);
    ttas_seeds = seed_rand();

    uint32_t* limits;
    limits = (uint32_t*)malloc(size * sizeof(uint32_t));
    uint32_t i;
    for (i = 0; i < size; i++) {
        limits[i]=1;
    }
    return limits;
}
Example #23
0
void checkChannel ( int channel ) {

	set_cpu(CPU_USED);

	while(1){
		while(io_read(channel))  // wait
		{}
	
		io_write(channel,LOW);
		usleep(5);
		io_write(channel,HIGH);
	}
}
Example #24
0
clh_local_params init_clh_local(uint32_t thread_num) {
    set_cpu(thread_num);

    //init its qnodes
    uint32_t i;
    clh_local_params local_params;
    //        local_params[i]=(clh_local_params*)malloc(sizeof(clh_local_params));
    local_params.my_qnode = (clh_qnode*) malloc(sizeof(clh_qnode));
    local_params.my_qnode->locked=0;
    local_params.my_pred = NULL;
    return local_params;

}
void *test(void *data)
{
    int rand_max;
    thread_data_t *d = (thread_data_t *)data;
    uint64_t res;
    phys_id= the_cores[d->id];
    set_cpu(phys_id);

    seeds = seed_rand();
    rand_max = num_entries - 1;

    /* Init of local data if necessary */

    /* Wait on barrier */
    barrier_cross(d->barrier);
    int entry;

    while (stop == 0) {
        if (num_entries==1) {
            entry=0;
        } else {
            entry =(int) my_random(&(seeds[0]),&(seeds[1]),&(seeds[2])) & rand_max;
        }
        //   entry = (int)(erand48(seed) * rand_max) + rand_min;
#ifdef TEST_CAS
        do {
            res = CAS_U8(&(the_data[entry].data),0,1);
        } while(res!=0);
#elif defined(TEST_TAS)
        do {
            res = TAS_U8(&(the_data[entry].data));
        } while (res!=0);
#elif defined(TEST_FAI)
        FAI_U8(&(the_data[entry].data));
#else
        perror("No test primitive specified");
#endif 
        MEM_BARRIER;
        the_data[entry].data = 0;
        d->num_operations++;
        if (op_pause>0) {
            cpause(op_pause);
        }
    }

    /* Free any local data if necessary */ 

    return NULL;
}
int main(int argc, char *argv[])
{
    sigset_t allsigs;
    int cur_val = 0;
	int s32CPUNo = 0;
	int s32CHDNo = 0;
	int s32i = 0;
	int s32pid = 0;

	if(argc != 3)
	{
		printf("Error!\n");
		exit(EXIT_FAILURE);
	}

	s32CPUNo = atoi(argv[1]);
	s32CHDNo = atoi(argv[2]) - 1;
	if(s32CPUNo < 0 || s32CPUNo > MAX_CORE_NO)
	{
		printf("OUT of Range\n");
		exit(EXIT_FAILURE);
	}

	printf("CPU bind: %d\n", s32CPUNo);
	set_cpu(s32CPUNo);
	printf("Set Sched:RR\n");
	set_sched();

	for(s32i = 0; s32i < s32CHDNo; s32i++)
	{
		s32pid = fork();
		if(0 == s32pid)
		{
			/* Child */
			break;
		}	
		else
		{
			/* Parent */
		}
	}
	

	for(s32i = 0; s32i < IRT_NUM; s32i++)
	{
	}
    
    exit(EXIT_SUCCESS);
}
Example #27
0
clh_local_params* init_clh_array_local(uint32_t thread_num, uint32_t num_locks) {
    set_cpu(thread_num);

    //init its qnodes
    uint32_t i;
    clh_local_params* local_params = (clh_local_params*)malloc(num_locks * sizeof(clh_local_params));
    for (i=0; i<num_locks; i++) {
        //        local_params[i]=(clh_local_params*)malloc(sizeof(clh_local_params));
        local_params[i].my_qnode = (clh_qnode*) malloc(sizeof(clh_qnode));
        local_params[i].my_qnode->locked=0;
        local_params[i].my_pred = NULL;
    }
    return local_params;

}
Example #28
0
/*
 * Actually perform MSR operations.
 */
static int
cpuctl_do_msr(int cpu, cpuctl_msr_args_t *data, u_long cmd, struct thread *td)
{
	uint64_t reg;
	int is_bound = 0;
	int oldcpu;
	int ret;

	KASSERT(cpu >= 0 && cpu <= mp_maxid,
	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));

	/*
	 * Explicitly clear cpuid data to avoid returning stale
	 * info
	 */
	DPRINTF("[cpuctl,%d]: operating on MSR %#0x for %d cpu\n", __LINE__,
	    data->msr, cpu);
#ifdef __i386__
	if ((cpu_feature & CPUID_MSR) == 0)
		return (ENODEV);
#endif
	oldcpu = td->td_oncpu;
	is_bound = cpu_sched_is_bound(td);
	set_cpu(cpu, td);
	if (cmd == CPUCTL_RDMSR) {
		data->data = 0;
		ret = rdmsr_safe(data->msr, &data->data);
	} else if (cmd == CPUCTL_WRMSR) {
		ret = wrmsr_safe(data->msr, data->data);
	} else if (cmd == CPUCTL_MSRSBIT) {
		critical_enter();
		ret = rdmsr_safe(data->msr, &reg);
		if (ret == 0)
			ret = wrmsr_safe(data->msr, reg | data->data);
		critical_exit();
	} else if (cmd == CPUCTL_MSRCBIT) {
		critical_enter();
		ret = rdmsr_safe(data->msr, &reg);
		if (ret == 0)
			ret = wrmsr_safe(data->msr, reg & ~data->data);
		critical_exit();
	} else
		panic("[cpuctl,%d]: unknown operation requested: %lu",
		    __LINE__, cmd);
	restore_cpu(oldcpu, is_bound, td);
	return (ret);
}
Example #29
0
void* periodicTest(void* num_param) {
	int num = (long) num_param;
	set_cpu(CPU_CORE);
	io_write(num, 1);
	struct timespec next;
	clock_gettime(CLOCK_REALTIME, &next);
	while(1) {
		while (io_read(num));

		io_write(num, 0);
		
		
		clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &next, NULL);
		timespec_add_us(&next, 100);
		io_write(num, 1);
	}
}
Example #30
0
 int main (int argc, const char * argv[]) { 
 // insert code here... 
 struct apple test; 
 struct orange test1={{0},{0}}; 
 pthread_t ThreadA; 
 
 unsigned long long sum=0,index=0; 
 struct timeval tpstart,tpend; 
 float timeuse; 
 
 test.a= 0; 
 test.b= 0; 
 
 cpu_nums = sysconf(_SC_NPROCESSORS_CONF); 
 
 if(-1 == set_cpu(0)) 
 { 
 return -1; 
 } 
 
 gettimeofday(&tpstart,NULL); 
 
 pthread_create(&ThreadA,NULL,add,&test); 
 
 
 for(index=0;index<ORANGE_MAX_VALUE;index++) 
 { 
 sum+=test1.a[index]+test1.b[index]; 
 } 
 
 pthread_join(ThreadA,NULL); 
 
 gettimeofday(&tpend,NULL); 
 
 timeuse=MSECOND*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec; 


 timeuse/=MSECOND; 
 printf("main thread:%x,Used Time:%f\n",pthread_self(),timeuse); 
 
 printf("a = %llu,b = %llu,sum=%llu\n",test.a,test.b,sum); 
 
 return 0; 
 }