Esempio n. 1
0
int
set_thread_locality(void  *threadid, char *loc_type, char *loc_value, int debug, FILE *where) {
  int err, value;
  tid_t thread_id;

  NETPERF_DEBUG_ENTRY(debug,where);

  thread_id = *(tid_t *)(threadid);
  value = atoi(loc_value);
  err = bindprocessor(BINDTHREAD, thread_id, value);

  if (debug) {
	fprintf(where,
		"%s's call to bindprocessor with BINDTHREAD thread %d and value %d returned %d errno %d\n",
		__func__,
                thread_id,
		value,
                err,
                errno);
	fflush(where);
  }

  NETPERF_DEBUG_EXIT(debug,where);
  return(NPE_SUCCESS);
}
Esempio n. 2
0
/* CPU affinity mask buffer instead, as the present code will fail beyond 32 CPUs */
int set_cpu_affinity(unsigned int cpuid) {
  unsigned long mask = 0xffffffff;
  unsigned int len = sizeof(mask);
  int retValue = 0;
  int pid;

 #ifdef _WIN32
   HANDLE hProcess;
 #endif
 
#ifdef _WIN32
  SET_MASK(cpuid)
  hProcess = GetCurrentProcess();
  if (SetProcessAffinityMask(hProcess, mask) == 0) {
    return -1;
  }
#elif CMK_HAS_BINDPROCESSOR
  pid = getpid();
  if (bindprocessor(BINDPROCESS, pid, cpuid) == -1) return -1;
#else
#ifdef CPU_ALLOC
 if ( cpuid >= CPU_SETSIZE ) {
  cpu_set_t *cpusetp;
  size_t size;
  int num_cpus;
  num_cpus = cpuid + 1;
  cpusetp = CPU_ALLOC(num_cpus);
  if (cpusetp == NULL) {
    perror("set_cpu_affinity CPU_ALLOC");
    return -1;
  }
  size = CPU_ALLOC_SIZE(num_cpus);
  CPU_ZERO_S(size, cpusetp);
  CPU_SET_S(cpuid, size, cpusetp);
  if (sched_setaffinity(0, size, cpusetp) < 0) {
    perror("sched_setaffinity dynamically allocated");
    CPU_FREE(cpusetp);
    return -1;
  }
  CPU_FREE(cpusetp);
 } else
#endif
 {
  cpu_set_t cpuset;
  CPU_ZERO(&cpuset);
  CPU_SET(cpuid, &cpuset);
  /*SET_MASK(cpuid)*/

  /* PID 0 refers to the current process */
  /*if (sched_setaffinity(0, len, &mask) < 0) {*/
  if (sched_setaffinity(0, sizeof(cpuset), &cpuset) < 0) {
    perror("sched_setaffinity");
    return -1;
  }
 }
#endif

  return 0;
}
Esempio n. 3
0
void write_one_affinity(int a) {
  if (a >= 0) {
    int tid = thread_self();
    if (bindprocessor(BINDTHREAD, tid, a) < 0) {
      fatal("bindprocessor") ;
    }
  }  
}
Esempio n. 4
0
int
clear_own_locality(char *loc_type, int debug, FILE *where){
  int err;

  err = bindprocessor(BINDTHREAD, thread_self(), PROCESSOR_CLASS_ANY);

  return(NPE_SUCCESS);
}
Esempio n. 5
0
void force_one_affinity(int cpu, int a, int verbose, char *name) {
  if (a >= 0) {
    int tid = thread_self();
    int r ;
    do {
      r = bindprocessor(BINDTHREAD, tid, a) ;
    } while (r < 0) ;
  }  
}
Esempio n. 6
0
/**
 This routine will set affinity for the calling thread that has rank 'rank'.
 Ranks start with 0.

 If there are multiple instances of QUARK then affinity will be wrong: all ranks 0
 will be pinned to core 0.

 Also, affinity is not resotred when QUARK_Finalize() is called.
 */
int quark_setaffinity(int rank) {
#ifndef QUARK_AFFINITY_DISABLE
#if (defined QUARK_OS_LINUX)
    {
        cpu_set_t set;
        CPU_ZERO( &set );
        CPU_SET( rank, &set );

#if (defined HAVE_OLD_SCHED_SETAFFINITY)
        if( sched_setaffinity( 0, &set ) < 0 )
#else /* HAVE_OLD_SCHED_SETAFFINITY */
        if( sched_setaffinity( 0, sizeof(set), &set) < 0 )
#endif /* HAVE_OLD_SCHED_SETAFFINITY */
            {
                return QUARK_ERR_UNEXPECTED;
            }

        return QUARK_SUCCESS;
    }
#elif (defined QUARK_OS_MACOS)
    {
        thread_affinity_policy_data_t ap;
        int                           ret;

        ap.affinity_tag = 1; /* non-null affinity tag */
        ret = thread_policy_set( mach_thread_self(),
                                 THREAD_AFFINITY_POLICY,
                                 (integer_t*) &ap,
                                 THREAD_AFFINITY_POLICY_COUNT
            );
        if(ret != 0)
            return QUARK_ERR_UNEXPECTED;

        return QUARK_SUCCESS;
    }
#elif (defined QUARK_OS_WINDOWS)
    {
        DWORD mask = 1 << rank;

        if( SetThreadAffinityMask(GetCurrentThread(), mask) == 0)
            return QUARK_ERR_UNEXPECTED;

        return QUARK_SUCCESS;
    }
#elif (defined QUARK_OS_AIX)
    {
        tid_t self_ktid = thread_self ();
        bindprocessor(BINDTHREAD, self_ktid, rank);
        return QUARK_SUCCESS;
    }
#else
    return QUARK_ERR_NOT_SUPPORTED;
#endif
#endif /* QUARK_AFFINITY_DISABLE */
    return QUARK_ERR_NOT_SUPPORTED;
}
Esempio n. 7
0
static int ATL_setmyaffinity()
/*
 * Attempts to sets the affinity of an already-running thread.  The
 * aff_set flag is set to true whether we succeed or not (no point in
 * trying multiple times).
 * RETURNS: 0 on success, non-zero error code on error
 */
{
   int bindID;
   bindID = omp_get_thread_num();
   #ifdef ATL_RANK_IS_PROCESSORID
      bindID = bindID % ATL_AFF_NUMID;
   #else
      bindID = ATL_affinityIDs[bindID%ATL_AFF_NUMID];
   #endif
#ifdef ATL_PAFF_PLPA
   plpa_cpu_set_t cpuset;
   PLPA_CPU_ZERO(&cpuset);
   PLPA_CPU_SET(bindID, &cpuset);
   if (me->paff_set)
      return(0);
   me->paff_set = 1;
   return(plpa_sched_setaffinity((pid_t)0, sizeof(cpuset), &cpuset));
#elif defined(ATL_PAFF_PBIND)
   return(processor_bind(P_LWPID, P_MYID, bindID, NULL));
#elif defined(ATL_PAFF_SCHED)
   cpu_set_t cpuset;
   CPU_ZERO(&cpuset);
   CPU_SET(bindID, &cpuset);
   if (me->paff_set)
      return(0);
   me->paff_set = 1;
   return(sched_setaffinity(0, sizeof(cpuset), &cpuset));
#elif defined (ATL_PAFF_RUNON)
   if (me->paff_set)
      return(0);
   me->paff_set = 1;
   return(pthread_setrunon_np(bindID));
#elif defined(ATL_PAFF_BINDP)
   if (me->paff_set)
      return(0);
   me->paff_set = 1;
   return(bindprocessor(BINDTHREAD, thread_self(), bindID));
#elif defined(ATL_PAFF_CPUSET)  /* untried FreeBSD code */
   cpuset_t mycpuset;
   CPU_ZERO(&mycpuset);         /* no manpage, so guess works like linux */
   CPU_SET(bindID, &mycpuset);
   if (me->paff_set)
      return(0);
   me->paff_set = 1;
   return(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
                             sizeof(mycpuset), &mycpuset));
#endif
   return(0);
}
Esempio n. 8
0
bool threadBindToProcessor(threadid_t tid, int pnumber)
{
#if defined(_WIN32)

	HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, 0, tid);
	if (!hThread)
		return false;

	bool success = SetThreadAffinityMask(hThread, 1 << pnumber) != 0;

	CloseHandle(hThread);
	return success;

#elif (defined(__FreeBSD__) && (__FreeBSD_version >= 702106)) \
	|| defined(__linux) || defined(linux)

	cpu_set_t cpuset;

	CPU_ZERO(&cpuset);
	CPU_SET(pnumber, &cpuset);
	return pthread_setaffinity_np(tid, sizeof(cpuset), &cpuset) == 0;

#elif defined(__sun) || defined(sun)

	return processor_bind(P_LWPID, MAKE_LWPID_PTHREAD(tid),
		pnumber, NULL) == 0;

#elif defined(_AIX)

	return bindprocessor(BINDTHREAD, (tid_t)tid, pnumber) == 0;

#elif defined(__hpux) || defined(hpux)

	pthread_spu_t answer;

	return pthread_processor_bind_np(PTHREAD_BIND_ADVISORY_NP,
		&answer, pnumber, tid) == 0;

#elif defined(__APPLE__)

	struct thread_affinity_policy tapol;

	thread_port_t threadport = pthread_mach_thread_np(tid);
	tapol.affinity_tag = pnumber + 1;
	return thread_policy_set(threadport, THREAD_AFFINITY_POLICY,
		(thread_policy_t)&tapol, THREAD_AFFINITY_POLICY_COUNT) == KERN_SUCCESS;

#else

	return false;

#endif
}
Esempio n. 9
0
void *test7_thread(void *v_param)
{
    if(bindprocessor(BINDTHREAD, thread_self(), (cpu_t)v_param) != 0) {
	printf("Error binding thread to processor %d, errno = %d.\n",
	       (int)v_param, errno);
    }

    while(!done) {
	DO_YIELD;
    }

    return NULL;
}
Esempio n. 10
0
bool Thread::bindToProcessor(unsigned int proc_number)
{
#if defined(__ANDROID__)

	return false;

#elif defined(_WIN32)

	return SetThreadAffinityMask(m_thread_handle, 1 << proc_number);

#elif __FreeBSD_version >= 702106 || defined(__linux) || defined(linux)

	cpu_set_t cpuset;

	CPU_ZERO(&cpuset);
	CPU_SET(proc_number, &cpuset);

	return pthread_setaffinity_np(m_thread_handle, sizeof(cpuset), &cpuset) == 0;

#elif defined(__sun) || defined(sun)

	return processor_bind(P_LWPID, P_MYID, proc_number, NULL) == 0

#elif defined(_AIX)

	return bindprocessor(BINDTHREAD, m_kernel_thread_id, proc_number) == 0;

#elif defined(__hpux) || defined(hpux)

	pthread_spu_t answer;

	return pthread_processor_bind_np(PTHREAD_BIND_ADVISORY_NP,
			&answer, proc_number, m_thread_handle) == 0;

#elif defined(__APPLE__)

	struct thread_affinity_policy tapol;

	thread_port_t threadport = pthread_mach_thread_np(m_thread_handle);
	tapol.affinity_tag = proc_number + 1;
	return thread_policy_set(threadport, THREAD_AFFINITY_POLICY,
			(thread_policy_t)&tapol,
			THREAD_AFFINITY_POLICY_COUNT) == KERN_SUCCESS;

#else

	return false;

#endif
}
Esempio n. 11
0
int
main (void)
{
    printf("##############################################################\n"
	   "# K42 Threading Microbenchmarks\n"
	   "# AIX Run\n"
	   "##############################################################\n");

    if(bindprocessor(BINDPROCESS, getpid(), 0) != 0) {
	printf("Bindprocessor failed.\n");
	return 8;
    }

    test1();
    test2();
    test3();
    return 0;
}
Esempio n. 12
0
/* TODO: seems to be right, but doesn't seem to be working (EINVAL), even after
 * aligning the range on 64K... */
static int
hwloc_aix_set_area_membind(hwloc_topology_t topology, const void *addr, size_t len, hwloc_const_nodeset_t nodeset, hwloc_membind_policy_t policy, int flags)
{
  subrange_t subrange;
  rsid_t rsid = { .at_subrange = &subrange };
  uint_t aix_policy;
  int ret;
  fprintf(stderr,"yop\n");

  if ((flags & (HWLOC_MEMBIND_MIGRATE|HWLOC_MEMBIND_STRICT))
            == (HWLOC_MEMBIND_MIGRATE|HWLOC_MEMBIND_STRICT)) {
    errno = ENOSYS;
    return -1;
  }

  subrange.su_offset = (uintptr_t) addr;
  subrange.su_length = len;
  subrange.su_rstype = R_RSET;

  if (hwloc_aix_membind_policy_from_hwloc(&aix_policy, policy))
    return -1;

  if (hwloc_aix_prepare_membind(topology, &subrange.su_rsid.at_rset, nodeset, flags))
    return -1;

  subrange.su_policy = aix_policy;

  res = ra_attachrset(R_SUBRANGE, rsid, subrange.su_rsid.at_rset, 0);
  if (res < 0 && errno == EPERM) {
    /* EPERM may mean that one thread has ben bound with bindprocessor().
     * Unbind the entire process (we can't unbind individual threads)
     * and try again.
     * FIXME: actually check that this EPERM can happen
     */
    bindprocessor(BINDPROCESS, getpid(), PROCESSOR_CLASS_ANY);
    res = ra_attachrset(R_SUBRANGE, rsid, subrange.su_rsid.at_rset, 0);
  }

  rs_free(subrange.su_rsid.at_rset);
  return ret;
}
Esempio n. 13
0
static int
hwloc_aix_set_sth_cpubind(hwloc_topology_t topology, rstype_t what, rsid_t who, pid_t pid, hwloc_const_bitmap_t hwloc_set, int flags __hwloc_attribute_unused)
{
  rsethandle_t rad;
  int res;
  unsigned cpu;

  if (flags & HWLOC_CPUBIND_NOMEMBIND) {
    errno = ENOSYS;
    return -1;
  }

  /* The resulting binding is always strict */

  if (hwloc_bitmap_isequal(hwloc_set, hwloc_topology_get_complete_cpuset(topology))) {
    if (ra_detachrset(what, who, 0))
      return -1;
    return 0;
  }

  rad = rs_alloc(RS_EMPTY);
  hwloc_bitmap_foreach_begin(cpu, hwloc_set)
    rs_op(RS_ADDRESOURCE, rad, NULL, R_PROCS, cpu);
  hwloc_bitmap_foreach_end();

  res = ra_attachrset(what, who, rad, 0);
  if (res < 0 && errno == EPERM) {
    /* EPERM may mean that one thread has ben bound with bindprocessor().
     * Unbind the entire process (we can't unbind individual threads)
     * and try again.
     */
    bindprocessor(BINDPROCESS, pid, PROCESSOR_CLASS_ANY);
    res = ra_attachrset(what, who, rad, 0);
  }

  rs_free(rad);
  return res;
}
Esempio n. 14
0
static int
hwloc_aix_set_sth_membind(hwloc_topology_t topology, rstype_t what, rsid_t who, pid_t pid, hwloc_const_bitmap_t nodeset, hwloc_membind_policy_t policy, int flags)
{
  rsethandle_t rad;
  int res;

  if (flags & HWLOC_MEMBIND_NOCPUBIND) {
    errno = ENOSYS;
    return -1;
  }

  switch (policy) {
    case HWLOC_MEMBIND_DEFAULT:
    case HWLOC_MEMBIND_BIND:
      break;
    default:
      errno = ENOSYS;
      return -1;
  }

  if (hwloc_aix_prepare_membind(topology, &rad, nodeset, flags))
    return -1;

  res = ra_attachrset(what, who, rad, 0);
  if (res < 0 && errno == EPERM) {
    /* EPERM may mean that one thread has ben bound with bindprocessor().
     * Unbind the entire process (we can't unbind individual threads)
     * and try again.
     */
    bindprocessor(BINDPROCESS, pid, PROCESSOR_CLASS_ANY);
    res = ra_attachrset(what, who, rad, 0);
  }

  rs_free(rad);
  return res;
}
Esempio n. 15
0
static int make_child(server_rec *s, int slot)
{
    int pid;

    if (slot + 1 > ap_max_daemons_limit) {
	ap_max_daemons_limit = slot + 1;
    }

    if (one_process) {
	apr_signal(SIGHUP, just_die);
        /* Don't catch AP_SIG_GRACEFUL in ONE_PROCESS mode :) */
	apr_signal(SIGINT, just_die);
#ifdef SIGQUIT
	apr_signal(SIGQUIT, SIG_DFL);
#endif
	apr_signal(SIGTERM, just_die);
	child_main(slot);
    }

    (void) ap_update_child_status_from_indexes(slot, 0, SERVER_STARTING,
                                               (request_rec *) NULL);


#ifdef _OSD_POSIX
    /* BS2000 requires a "special" version of fork() before a setuid() call */
    if ((pid = os_fork(unixd_config.user_name)) == -1) {
#elif defined(TPF)
    if ((pid = os_fork(s, slot)) == -1) {
#else
    if ((pid = fork()) == -1) {
#endif
	ap_log_error(APLOG_MARK, APLOG_ERR, errno, s, "fork: Unable to fork new process");

	/* fork didn't succeed. Fix the scoreboard or else
	 * it will say SERVER_STARTING forever and ever
	 */
	(void) ap_update_child_status_from_indexes(slot, 0, SERVER_DEAD,
                                                   (request_rec *) NULL);

	/* In case system resources are maxxed out, we don't want
	   Apache running away with the CPU trying to fork over and
	   over and over again. */
	sleep(10);

	return -1;
    }

    if (!pid) {
#ifdef HAVE_BINDPROCESSOR
        /* by default AIX binds to a single processor
         * this bit unbinds children which will then bind to another cpu
         */
	int status = bindprocessor(BINDPROCESS, (int)getpid(), 
				   PROCESSOR_CLASS_ANY);
	if (status != OK) {
	    ap_log_error(APLOG_MARK, APLOG_WARNING, errno, 
                         ap_server_conf, "processor unbind failed %d", status);
	}
#endif
	RAISE_SIGSTOP(MAKE_CHILD);
        AP_MONCONTROL(1);
        /* Disable the parent's signal handlers and set up proper handling in
         * the child.
	 */
	apr_signal(SIGHUP, just_die);
	apr_signal(SIGTERM, just_die);
        /* The child process doesn't do anything for AP_SIG_GRACEFUL.  
         * Instead, the pod is used for signalling graceful restart.
         */
        apr_signal(AP_SIG_GRACEFUL, SIG_IGN);
	child_main(slot);
    }

    ap_scoreboard_image->parent[slot].pid = pid;

    return 0;
}


/* start up a bunch of children */
static void startup_children(int number_to_start)
{
    int i;

    for (i = 0; number_to_start && i < ap_daemons_limit; ++i) {
	if (ap_scoreboard_image->servers[i][0].status != SERVER_DEAD) {
	    continue;
	}
	if (make_child(ap_server_conf, i) < 0) {
	    break;
	}
	--number_to_start;
    }
}


/*
 * idle_spawn_rate is the number of children that will be spawned on the
 * next maintenance cycle if there aren't enough idle servers.  It is
 * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
 * without the need to spawn.
 */
static int idle_spawn_rate = 1;
#ifndef MAX_SPAWN_RATE
#define MAX_SPAWN_RATE	(32)
#endif
static int hold_off_on_exponential_spawning;

static void perform_idle_server_maintenance(apr_pool_t *p)
{
    int i;
    int to_kill;
    int idle_count;
    worker_score *ws;
    int free_length;
    int free_slots[MAX_SPAWN_RATE];
    int last_non_dead;
    int total_non_dead;

    /* initialize the free_list */
    free_length = 0;

    to_kill = -1;
    idle_count = 0;
    last_non_dead = -1;
    total_non_dead = 0;

    for (i = 0; i < ap_daemons_limit; ++i) {
	int status;

	if (i >= ap_max_daemons_limit && free_length == idle_spawn_rate)
	    break;
	ws = &ap_scoreboard_image->servers[i][0];
	status = ws->status;
	if (status == SERVER_DEAD) {
	    /* try to keep children numbers as low as possible */
	    if (free_length < idle_spawn_rate) {
		free_slots[free_length] = i;
		++free_length;
	    }
	}
	else {
	    /* We consider a starting server as idle because we started it
	     * at least a cycle ago, and if it still hasn't finished starting
	     * then we're just going to swamp things worse by forking more.
	     * So we hopefully won't need to fork more if we count it.
	     * This depends on the ordering of SERVER_READY and SERVER_STARTING.
	     */
	    if (status <= SERVER_READY) {
		++ idle_count;
		/* always kill the highest numbered child if we have to...
		 * no really well thought out reason ... other than observing
		 * the server behaviour under linux where lower numbered children
		 * tend to service more hits (and hence are more likely to have
		 * their data in cpu caches).
		 */
		to_kill = i;
	    }

	    ++total_non_dead;
	    last_non_dead = i;
	}
    }
    ap_max_daemons_limit = last_non_dead + 1;
    if (idle_count > ap_daemons_max_free) {
	/* kill off one child... we use the pod because that'll cause it to
	 * shut down gracefully, in case it happened to pick up a request
	 * while we were counting
	 */
	ap_mpm_pod_signal(pod);
	idle_spawn_rate = 1;
    }
    else if (idle_count < ap_daemons_min_free) {
	/* terminate the free list */
	if (free_length == 0) {
	    /* only report this condition once */
	    static int reported = 0;

	    if (!reported) {
		ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
			    "server reached MaxClients setting, consider"
			    " raising the MaxClients setting");
		reported = 1;
	    }
	    idle_spawn_rate = 1;
	}
	else {
	    if (idle_spawn_rate >= 8) {
		ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
		    "server seems busy, (you may need "
		    "to increase StartServers, or Min/MaxSpareServers), "
		    "spawning %d children, there are %d idle, and "
		    "%d total children", idle_spawn_rate,
		    idle_count, total_non_dead);
	    }
	    for (i = 0; i < free_length; ++i) {
#ifdef TPF
        if (make_child(ap_server_conf, free_slots[i]) == -1) {
            if(free_length == 1) {
                shutdown_pending = 1;
                ap_log_error(APLOG_MARK, APLOG_EMERG, 0, ap_server_conf,
                "No active child processes: shutting down");
            }
        }
#else
		make_child(ap_server_conf, free_slots[i]);
#endif /* TPF */
	    }
	    /* the next time around we want to spawn twice as many if this
	     * wasn't good enough, but not if we've just done a graceful
	     */
	    if (hold_off_on_exponential_spawning) {
		--hold_off_on_exponential_spawning;
	    }
	    else if (idle_spawn_rate < MAX_SPAWN_RATE) {
		idle_spawn_rate *= 2;
	    }
	}
    }
    else {
	idle_spawn_rate = 1;
    }
}
Esempio n. 16
0
int
read_hardware_config(SYS_CONF_TYP * scfg, unsigned int tot_cpus, unsigned int pvr)  {

    int i, j = 0;
    int bind_to_cpu , rc, pir;
	int node, chip , proc;
    char msg[4096];
	SYS_CONF_TYP physical_scfg ;
	int num_nodes = 0, chips_per_node = 0;

    for(i = 0; i < MAX_CPUS ; i++) {
        l_p[i] = NO_CPU_DEFINED;
    }
#ifndef __HTX_LINUX__
    for(i = 0 ; i < tot_cpus ; i++ ) {
        bind_to_cpu = i ;
       	rc = bindprocessor(BINDPROCESS, getpid(), bind_to_cpu);
        if(rc == -1) {
            sprintf(msg, "\n bindprocessor failed with rc = %d, i = %d, errno = %d", rc, i, errno);
			hxfmsg(&htx_d, 0, HTX_HE_HARD_ERROR, msg);
            return(-1);
        }

        pir = getPir();
        l_p[i] = pir ;

       	rc = bindprocessor(BINDPROCESS, getpid(), PROCESSOR_CLASS_ANY);
        if(rc == -1) {
            sprintf( msg, "\n%d: bindprocessor failed with %d while unbinding, rc = %d, i = %d ",__LINE__,  errno, rc, i);
			hxfmsg(&htx_d, 0, HTX_HE_HARD_ERROR, msg);
            return(-1);
        }
    }
#else
	FILE *fp;
	char command[200],fname[256];
	int rad, lcpu, nrads, num_procs;

	strcpy(fname,htx_d.htx_exer_log_dir);
	sprintf(fname,"%s/node_details.%d",fname,getpid());
    /*sprintf(command,"/usr/lpp/htx/etc/scripts/get_node_details.sh "
            "> %s\n",fname);*/
    strcpy(command,getenv("HTXSCRIPTS"));
    sprintf(command, "%s/get_node_details.sh > %s\n", command, fname);

    if ( (rc = system(command)) == -1 ) {
            sprintf(msg, "sytem command to get_node_details failed with %d",rc);
            hxfmsg(&htx_d, rc, HTX_HE_HARD_ERROR, msg);
        	return(-1);
    }
    if ((fp=fopen(fname,"r"))==NULL){
            sprintf(msg, "fopen of file %s failed with errno=%d",fname,errno);
            hxfmsg(&htx_d, rc, HTX_HE_HARD_ERROR, msg);
        	return(-1);
    }

    /* Get total number of chips */
    rc = fscanf(fp,"num_nodes=%d\n",&nrads);
    if (rc == 0 || rc == EOF) {
            sprintf(msg, "fscanf of num_nodes on file %s failed with errno =%d",fname,errno);
            hxfmsg(&htx_d, rc, HTX_HE_HARD_ERROR, msg);
            return(-1);
    }
    for (rad = 0; rad < nrads; rad++) {
            int cpu_index = 0;
            int cur_chip = -1; /* Current chip */
            /* Fetch the current chip and the num procs in the chip */
            rc = fscanf(fp,"node=%d,cpus_in_node=%d,cpus",&cur_chip,&num_procs);
            if (rc == 0 || rc == EOF) {
                 sprintf(msg, "fscanf: chip =%d,Fetching of cpus in chip error from"
                        " file %s failed with errno =%d",rad,fname,errno);
                 hxfmsg(&htx_d, rc, HTX_HE_HARD_ERROR, msg);
                 return(-1);
            }

            if ( num_procs > tot_cpus ) {
                sprintf(msg, "num_procs %d in this chip cannot be more than max procs %d "
                        ,num_procs,tot_cpus);
                hxfmsg(&htx_d,0,HTX_HE_HARD_ERROR, msg);
                return(-1);
            }

           /*
            * To find the actual processors (logical cpu Ids that can be
            * specified in an rset):
            */
            cpu_index = 0 ;

            while ( cpu_index < num_procs) {
                rc = fscanf(fp,":%d",&lcpu);
                if ( rc == 0 || rc == EOF) {
                    sprintf(msg, "fscanf: cpu fetch for chip=%d caused error from "
                                 " file %s failed with errno =%d",rad,fname,errno);
                    hxfmsg(&htx_d, rc, HTX_HE_HARD_ERROR, msg);
                    return(-1);
                }
				/* Bind to lcpu and get the PIR value for it */
				rc = htx_bind_process(BIND_THE_PROCESS, lcpu);
				if(rc == -1) {
		            sprintf(msg, "\n bindprocessor failed with %d, binding cpu=%d", errno, lcpu);
            		hxfmsg(&htx_d, errno, HTX_HE_HARD_ERROR, msg);
             		return(-1);
        		}

                l_p[lcpu] = get_cpu_id(lcpu);
				rc = htx_unbind_process();
				if(rc == -1) {
					sprintf(msg, "\n bindprocessor failed with %d, unbinding th=%d, cpu=%d", errno, lcpu);
					hxfmsg(&htx_d, errno, HTX_HE_HARD_ERROR, msg);
					return(-1);
				}

				DEBUGON("l_p[%d]=%d\n",lcpu, l_p[lcpu]);
                cpu_index++;
            }
            rc = fscanf(fp,"\n");
            if (rc == EOF) {
                   sprintf(msg, "fscanf: expecting new line character at the end of"
                                " node %d data reading from file %s failed with errno =%d",\
                                rad,fname,errno);
                   hxfmsg(&htx_d, rc, HTX_HE_HARD_ERROR, msg);
                   return(-1);
            }
     }

#endif
	/* Intialize the sysconf data structure */
	for(node = 0; node < MAX_NODES; node++) {
		for(chip = 0; chip < MAX_CHIPS_PER_NODE; chip ++) {
			for(proc = 0; proc < MAX_CPUS_PER_CHIP; proc ++) {
				physical_scfg.node[node].chip[chip].lprocs[proc] = -1;
			}
			physical_scfg.node[node].chip[chip].num_procs = 0;
		}
		physical_scfg.node[node].num_chips = 0;
	}
	physical_scfg.num_nodes = 0 ;

    i = 0 ;
   	while(i < MAX_CPUS) {
		if(l_p[i] == NO_CPU_DEFINED)  {
			i++;
			continue;
		}
        if(pvr == PVR_POWER9_NIMBUS){
            node = P9_NIMBUS_GET_NODE(l_p[i]);
            chip = P9_NIMBUS_GET_CHIP(l_p[i]);
        } else if(pvr == PVR_POWER9_CUMULUS){
            node = P9_CUMULUS_GET_NODE(l_p[i]);
            chip = P9_CUMULUS_GET_CHIP(l_p[i]);
        } else if(pvr == PVR_POWER8_MURANO || pvr == PVR_POWER8_VENICE || pvr == PVR_POWERP8P_GARRISION) {
			node = P8_GET_NODE(l_p[i]);
			chip = P8_GET_CHIP(l_p[i]);
    	} else if(pvr == PVR_POWER7 || pvr == PVR_POWER7PLUS) {
        	node = P7_GET_NODE(l_p[i]);
        	chip = P7_GET_CHIP(l_p[i]);
    	} else if(pvr == PVR_POWER6) {
			node = P6_GET_NODE(l_p[i]);
			chip = P6_GET_CHIP(l_p[i]);
		} else {
			sprintf(msg, "\n We shldnt have been here pvr = %x,  PVR mismatch. Exiting !!\n", pvr);
            hxfmsg(&htx_d, 0, HTX_HE_HARD_ERROR, msg);
            return(-1);
		}
		DEBUGON("Node=%d,Chip%d,proc=%d\n",node,chip,i);
       	physical_scfg.node[node].chip[chip].lprocs[physical_scfg.node[node].chip[chip].num_procs++] = i ;
       	i++;
    }

	for(i = 0; i < MAX_NODES; i ++) {
		for( j = 0; j < MAX_CHIPS_PER_NODE; j++) {
			if(physical_scfg.node[i].chip[j].num_procs > 0) {
				physical_scfg.node[i].num_chips ++;
			}
		}
		if(physical_scfg.node[i].num_chips > 0) {
			physical_scfg.num_nodes ++;
		}
	}

	/* Copy the system configuration to scfg */
	for(node = 0; node < MAX_NODES; node++) {
		if(physical_scfg.node[node].num_chips == 0)
			continue;
		chips_per_node = 0;
		for(chip = 0; chip < MAX_CHIPS_PER_NODE; chip ++) {

			if(physical_scfg.node[node].chip[chip].num_procs == 0)
				continue;
			memcpy(&scfg->node[num_nodes].chip[chips_per_node], &physical_scfg.node[node].chip[chip], sizeof(CHIP));
			chips_per_node++;
		}
		if(chips_per_node != physical_scfg.node[node].num_chips) {
			sprintf(msg, "%s: Something Wrong !!! Node = %d, chips_per_node = %d, physical_scfg_chip = %d \n",
													 __FUNCTION__,node, chips_per_node, physical_scfg.node[node].num_chips) ;
			hxfmsg(&htx_d, 0, HTX_HE_HARD_ERROR, msg);
            return(-1);
        }
		scfg->node[num_nodes].num_chips = physical_scfg.node[node].num_chips;
		num_nodes++;
	}
	if(num_nodes != physical_scfg.num_nodes) {
		sprintf(msg, "%s: Something Wrong !!! Num_nodes = %d, physical_num_nodes = %d \n",
										__FUNCTION__, num_nodes, physical_scfg.num_nodes);
		hxfmsg(&htx_d, 0, HTX_HE_HARD_ERROR, msg);
        return(-1);
    }

	scfg->num_nodes = physical_scfg.num_nodes;

    return 0 ;
}
Esempio n. 17
0
int
get_cont_phy_mem(unsigned int num_cpus_mapped, unsigned int memory_mapping[][2], long long size, MEMORY_SET mem[]) {

    char *p, *addr, msg[4096];
    int i=0, ii, rc, host_cpu , j;
    size_t shm_size;
    unsigned int cpu, bind_to_cpu;
    unsigned int memflg;

#ifndef __HTX_LINUX__ /* AIX Specific */

    psize_t psize = 16*M;
    struct shmid_ds shm_buf = { 0 };
	struct vminfo_psize vminfo_64k = { 0 }; 

    memflg = (IPC_CREAT | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
    if(SET_PAGESIZE) {
        psize = 16*M;
    } else {
        psize = 4 * K;
    }
#else   /* Linux Specific */
    unsigned long long psize;

    memflg = (IPC_CREAT | IPC_EXCL |SHM_R | SHM_W);
    if(SET_PAGESIZE) {
        memflg |= (SHM_HUGETLB);
        psize = 16*M;
    } else {
        psize = 4 * K;
    }

    /* shmmax controls the maximum amount of memory to be allocated for shared memory */
    rc = system ("echo 268435456 > /proc/sys/kernel/shmmax");
    if (rc < 0){
        sprintf(msg,"unable to change the /proc/sys/kernel/shmmax variable\n");
        hxfmsg(&htx_d, 0, HTX_HE_INFO, msg);
    }

    /* The maximum amount of shared memory that can be allocated. */
    rc = system ("echo 268435456 > /proc/sys/kernel/shmall");
    if (rc < 0) {
        sprintf(msg,"unable to change the /proc/sys/kernel/shmall");
        hxfmsg(&htx_d, 0, HTX_HE_INFO, msg);
    }
#endif


    DEBUGON("\n Need %llx physically contiguous memory \n", size);

    shm_size = (size < PG_SZ_16M ? PG_SZ_16M : size);

    for(cpu = 0; cpu < num_cpus_mapped; cpu ++) {

    	if(memory_mapping[cpu][DEST_CPU] != NO_CPU_DEFINED) {
            host_cpu = memory_mapping[cpu][HOST_CPU];
            bind_to_cpu = memory_mapping[cpu][DEST_CPU];
        } else {
            continue;
        }
#ifdef __HTX_LINUX__
	    rc = htx_bind_process(BIND_THE_PROCESS, bind_to_cpu);
#else
   		rc = bindprocessor(BINDPROCESS, getpid(), bind_to_cpu);
#endif
        if(rc == -1) {
            sprintf(msg, "\n bindprocessor failed with %d, binding th=%d, cpu=%d", errno, cpu,bind_to_cpu);
            hxfmsg(&htx_d, errno, HTX_HE_HARD_ERROR, msg);
            return(-1);
        }

        mem[host_cpu].shm_id = shmget(IPC_PRIVATE, shm_size, memflg);
        if(mem[host_cpu].shm_id == -1) {
            memory_set_cleanup();
            sprintf(msg,"shmget failed with %d !\n", errno);
            hxfmsg(&htx_d, rc, HTX_HE_HARD_ERROR, msg);
            return(-1);
        }

#ifndef __HTX_LINUX__
	if(SET_PAGESIZE) {  
        if ( shm_size > 256*M) {
            if ((rc = shmctl(mem[host_cpu].shm_id, SHM_GETLBA, &shm_buf)) == -1)   {
                memory_set_cleanup();
                sprintf(msg,"\n shmctl failed to get minimum alignment requirement - %d", errno);
                hxfmsg(&htx_d, rc, HTX_HE_HARD_ERROR, msg);
                return(-1);
            }
        }
        shm_buf.shm_pagesize = psize;
        if( (rc = shmctl(mem[host_cpu].shm_id, SHM_PAGESIZE, &shm_buf)) == -1) {
            memory_set_cleanup();
            sprintf(msg,"\n shmctl failed with %d while setting page size.",errno);
            hxfmsg(&htx_d, rc, HTX_HE_HARD_ERROR, msg);
            return(-1);
        }
	}
#endif
        addr = (char *) shmat(mem[host_cpu].shm_id, 0, 0);
        if(-1 == (int)addr) {
            sprintf(msg,"\n shmat failed with %d",errno);
            hxfmsg(&htx_d, -1,HTX_HE_HARD_ERROR, msg);
            memory_set_cleanup();
            return(-1);
        }

        /* to be on safer side write touch the remote memory obtained */
        char * mem_addr = addr;
        for(i=0; i < (shm_size/psize); i++) {
            *(unsigned long long *)mem_addr = 0xFFFFffffFFFFffff ; 
            mem_addr += psize;
        }

        sprintf(msg, "%s: cpu %d, bound to cpu %d, seg_addr = 0x%llx of size = 0x%llx\n",
									__FUNCTION__, host_cpu, bind_to_cpu, (unsigned long long *)addr, shm_size);
        hxfmsg(&htx_d, 0, HTX_HE_INFO, msg);

		/* Lock the new shared memory, so that its always memory resident */ 
        rc = mlock(addr, shm_size);
        if(rc == -1) {
            sprintf(msg,"\n mlock failed with %d", errno);
            hxfmsg(&htx_d, rc, HTX_HE_HARD_ERROR, msg);
            memory_set_cleanup();
            return(-1);
        }

        mem[host_cpu].seg_addr = addr;
		mem[host_cpu].memory_set_size = shm_size; 

#ifdef __HTX_LINUX__
	   	rc = htx_unbind_process();
#else
    	rc = bindprocessor(BINDPROCESS, getpid(), PROCESSOR_CLASS_ANY);
#endif
        if(rc == -1) {
            sprintf(msg, "\n%d: bindprocessor failed with %d while unbinding",__LINE__,  errno);
            hxfmsg(&htx_d, errno, HTX_HE_HARD_ERROR, msg);
            return(-1);
        }
    } /* Off num_cpus_mapped loop */
    return(0);
}
Esempio n. 18
0
void *DumbTest(void *vp)
{
   assert(!bindprocessor(BINDTHREAD, thread_self(), rank));
   pthread_exit(vp);
}
PASTIX_INT sopalin_bindthread(PASTIX_INT cpu)
{
#ifdef MARCEL

  {
    marcel_vpset_t vpset = MARCEL_VPSET_ZERO;
    marcel_vpset_vp(&vpset, cpu);
    marcel_apply_vpset(&vpset);
  }

#else /* Dans les autres cas on se preoccupe de l'archi */

#ifdef WITH_HWLOC
  {
    hwloc_topology_t topology; /* Topology object */
    hwloc_obj_t      obj;      /* Hwloc object    */
    hwloc_cpuset_t   cpuset;   /* HwLoc cpuset    */

    /* Allocate and initialize topology object.  */
    hwloc_topology_init(&topology);

    /* Perform the topology detection.  */
    hwloc_topology_load(topology);

    /* Get last one.  */
    obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_CORE, cpu);
    if (!obj)
      return 0;

    /* Get a copy of its cpuset that we may modify.  */
    /* Get only one logical processor (in case the core is SMT/hyperthreaded).  */
#if !defined(HWLOC_BITMAP_H)
    cpuset = hwloc_cpuset_dup(obj->cpuset);
    hwloc_cpuset_singlify(cpuset);
#else
    cpuset = hwloc_bitmap_dup(obj->cpuset);
    hwloc_bitmap_singlify(cpuset);
#endif

    /* And try to bind ourself there.  */
    if (hwloc_set_cpubind(topology, cpuset, HWLOC_CPUBIND_THREAD)) {
      char *str = NULL;
#if !defined(HWLOC_BITMAP_H)
      hwloc_cpuset_asprintf(&str, obj->cpuset);
#else
      hwloc_bitmap_asprintf(&str, obj->cpuset);
#endif
      printf("Couldn't bind to cpuset %s\n", str);
      free(str);
    }

    /* Get the number at Proc level */
    cpu = obj->children[0]->os_index;

    /* Free our cpuset copy */
#if !defined(HWLOC_BITMAP_H)
    hwloc_cpuset_free(cpuset);
#else
    hwloc_bitmap_free(cpuset);
#endif

    /* Destroy topology object.  */
    hwloc_topology_destroy(topology);
  }
#else /* WITH_HWLOC */
#ifdef X_ARCHpower_ibm_aix
  {
    tid_t self_ktid = thread_self ();

    bindprocessor(BINDTHREAD, self_ktid, cpu);
  }
#elif (defined X_ARCHalpha_compaq_osf1)
  {
    bind_to_cpu_id(getpid(), cpu, 0);
  }
#elif (defined X_ARCHi686_pc_linux)

#ifndef X_ARCHi686_mac
  {
    cpu_set_t mask;
    CPU_ZERO(&mask);
    CPU_SET(cpu, &mask);

#ifdef HAVE_OLD_SCHED_SETAFFINITY
    if(sched_setaffinity(0,&mask) < 0)
#else /* HAVE_OLD_SCHED_SETAFFINITY */
    if(sched_setaffinity(0,sizeof(mask),&mask) < 0)
#endif /* HAVE_OLD_SCHED_SETAFFINITY */
      {
  perror("sched_setaffinity");
  EXIT(MOD_SOPALIN, INTERNAL_ERR);
      }
  }
#else /* X_ARCHi686_mac */
  {
    thread_affinity_policy_data_t ap;
    int                           ret;

    ap.affinity_tag = 1; /* non-null affinity tag */
    ret = thread_policy_set(
          mach_thread_self(),
          THREAD_AFFINITY_POLICY,
          (integer_t*) &ap,
          THREAD_AFFINITY_POLICY_COUNT
          );
    if(ret != 0)
      {
  perror("thread_policy_set");
  EXIT(MOD_SOPALIN, INTERNAL_ERR);
      }
  }
#endif /* X_ARCHi686_mac */
#endif /* X_ACHIxxx      */
#endif /* WITH_HWLOC     */
#endif /* MARCEL         */

  return cpu;
}
Esempio n. 20
0
int set_thread_affinity(int cpuid) {
  unsigned long mask = 0xffffffff;
  unsigned int len = sizeof(mask);

#ifdef _WIN32
  HANDLE hThread;
#endif	
  
#ifdef _WIN32
  SET_MASK(cpuid)
  hThread = GetCurrentThread();
  if (SetThreadAffinityMask(hThread, mask) == 0) {
    return -1;
  }
#elif  CMK_HAS_PTHREAD_SETAFFINITY
#ifdef CPU_ALLOC
 if ( cpuid >= CPU_SETSIZE ) {
  cpu_set_t *cpusetp;
  pthread_t thread;
  size_t size;
  int num_cpus;
  num_cpus = cpuid + 1;
  cpusetp = CPU_ALLOC(num_cpus);
  if (cpusetp == NULL) {
    perror("set_thread_affinity CPU_ALLOC");
    return -1;
  }
  size = CPU_ALLOC_SIZE(num_cpus);
  thread = pthread_self();
  CPU_ZERO_S(size, cpusetp);
  CPU_SET_S(cpuid, size, cpusetp);
  if (errno = pthread_setaffinity_np(thread, size, cpusetp)) {
    perror("pthread_setaffinity dynamically allocated");
    CPU_FREE(cpusetp);
    return -1;
  }
  CPU_FREE(cpusetp);
 } else
#endif
 {
  int s, j;
  cpu_set_t cpuset;
  pthread_t thread;

  thread = pthread_self();

  CPU_ZERO(&cpuset);
  CPU_SET(cpuid, &cpuset);

  if (errno = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset)) {
    perror("pthread_setaffinity");
    return -1;
  }
 }
#elif CMK_HAS_BINDPROCESSOR
  if (bindprocessor(BINDTHREAD, thread_self(), cpuid) != 0)
    return -1;
#else
  return set_cpu_affinity(cpuid);
#endif

  return 0;
}
Esempio n. 21
0
int ATL_thread_start(ATL_thread_t *thr, int proc, int JOINABLE,
                     void *(*rout)(void*), void *arg)
/*
 * Creates a thread that will run only on processor proc.
 * RETURNS: 0 on success, non-zero on error
 * NOTE: present implementation dies on error, so 0 is always returned.
 */
{
#ifdef ATL_WINTHREADS
   #ifdef ATL_WIN32THREADS
      DWORD thrID;
   #else
      unsigned thrID;
   #endif

   #ifdef ATL_NOAFFINITY
      #ifdef ATL_WIN32THREADS
         thr->thrH = CreateThread(NULL, 0, rout, arg, 0, &thrID);
      #else
         thr->thrH = (HANDLE)_beginthreadex(NULL, 0, rout, arg, 0, &thrID);
      #endif
      ATL_assert(thr->thrH);
   #else
      thr->rank = proc;
      #ifdef ATL_WIN32THREADS
         thr->thrH = CreateThread(NULL, 0, rout, arg, CREATE_SUSPENDED, &thrID);
      #else
         thr->thrH = (HANDLE)_beginthreadex(NULL, 0, rout, arg,
                                            CREATE_SUSPENDED, &thrID);
      #endif
      ATL_assert(thr->thrH);
      #ifdef ATL_RANK_IS_PROCESSORID
         ATL_assert(SetThreadAffinityMask(thr->thrH, (1<<proc)));
      #else
         ATL_assert(SetThreadAffinityMask(thr->thrH,
                    (1<<ATL_affinityIDs[proc%ATL_AFF_NUMID])));
      #endif
      ATL_assert(ResumeThread(thr->thrH) == 1);
   #endif
#elif defined(ATL_OMP_THREADS)
   fprintf(stderr, "Should not call thread_start when using OpenMP!");
   ATL_assert(0);
#elif 0 && defined(ATL_OS_OSX)  /* unchecked special OSX code */
/* http://developer.apple.com/library/mac/#releasenotes/Performance/RN-AffinityAPI/_index.html */
   pthread_attr_t attr;
   #define ATL_OSX_AFF_SETS 2       /* should be probed for */
   thread_affinity_policy ap;

   ap.affinity_tag = proc % ATL_OSX_AFF_SETS;
   ATL_assert(!pthread_attr_init(&attr));
   if (JOINABLE)
      ATL_assert(!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
   else
      ATL_assert(!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
   pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); /* no chk, OK to fail */

   ATL_assert(!pthread_create(&thr->thrH, &attr, rout, arg));
   ATL_assert(!thread_policy_set(thr->thrH, THREAD_AFFINITY_POLICY,
                                 (integer_t*)&ap,
                                 THREAD_AFFINITY_POLICY_COUNT));
   ATL_assert(!pthread_attr_destroy(&attr));
#else
   pthread_attr_t attr;
   #ifndef ATL_NOAFFINITY
      #if defined(ATL_PAFF_SETAFFNP) || defined(ATL_PAFF_SCHED)
         cpu_set_t cpuset;
      #elif defined(ATL_PAFF_PLPA)
         plpa_cpu_set_t cpuset;
      #elif defined(ATL_PAFF_CPUSET) /* untried FreeBSD code */
         cpuset_t mycpuset;
      #endif
      #ifdef ATL_RANK_IS_PROCESSORID
         const int affID = proc;
      #else
         const int affID = ATL_affinityIDs[proc%ATL_AFF_NUMID];
      #endif
      #ifdef ATL_PAFF_SELF
         thr->paff_set = 0;  /* affinity must be set by created thread */
      #endif
   #endif
   thr->rank = proc;
   ATL_assert(!pthread_attr_init(&attr));
   if (JOINABLE)
   {
      #ifdef IBM_PT_ERROR
         ATL_assert(!pthread_attr_setdetachstate(&attr,
                                                 PTHREAD_CREATE_UNDETACHED));
      #else
         ATL_assert(!pthread_attr_setdetachstate(&attr,
                                                 PTHREAD_CREATE_JOINABLE));
      #endif
   }
   else
      ATL_assert(!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
   pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); /* no chk, OK to fail */
   #ifdef ATL_PAFF_SETAFFNP
      CPU_ZERO(&cpuset);
      CPU_SET(affID, &cpuset);
      ATL_assert(!pthread_attr_setaffinity_np(&attr, sizeof(cpuset), &cpuset));
   #elif defined(ATL_PAFF_SETPROCNP)
      ATL_assert(!pthread_attr_setprocessor_np(&attr, (pthread_spu_t)affID,
                                               PTHREAD_BIND_FORCED_NP));
   #endif
   ATL_assert(!pthread_create(&thr->thrH, &attr, rout, arg));
   #if defined(ATL_PAFF_PBIND)
      ATL_assert(!processor_bind(P_LWPID, thr->thrH, affID, NULL));
      thr->paff_set = 0;  /* affinity set by spawner */
   #elif defined(ATL_PAFF_BINDP)
      ATL_assert(!bindprocessor(BINDTHREAD, thr->thrH, bindID));
      thr->paff_set = 0;  /* affinity set by spawner */
   #elif defined(ATL_PAFF_CPUSET)  /* untried FreeBSD code */
      CPU_ZERO(&mycpuset);         /* no manpage, so guess works like linux */
      CPU_SET(bindID, &mycpuset);
      if (!cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, thr->thrH,
                              sizeof(mycpuset), &mycpuset));
         thr->paff_set = 0;  /* affinity set by spawner */
   #endif
   ATL_assert(!pthread_attr_destroy(&attr));
#endif
   return(0);
}
Esempio n. 22
0
void prefetch_irritator(void *arg)
{
    int i, rc, no_of_pages, tid , thread_no, tc, oper , number_of_operations;
    unsigned long long  saved_seed, random_no , starting_address , memory_fetch_size;
	pthread_t ptid;
	unsigned char *start_addr;
    struct thread_context *th = (struct thread_context *)arg;
 	struct ruleinfo *current_rule 	= th->current_rule;
	int cache_type = current_rule->tgt_cache;
	int cache_line_size = system_information.cinfo[cache_type].line_size;
	unsigned int loop_count 	;
	long int	offset;
	unsigned long long temp_storage = 0x1, temp_pattern = 0x1;

    /*
     * char *contig_mem[NUM_SEGS*SEG_SIZE/(16*M)]; Physically contiguous
     * memory pointer. memory_set_size variable  gives total memory
     * allocated both are variables of global structure.
     */

    thread_no = th->thread_no ;
	int pcpu = pcpus_thread_wise[thread_no];
    tid       = th->bind_to_cpu;    /* Bind to the processor */
	ptid	  = th->tid;			/* PThread Id for this thread	*/
	prefetch_streams 	= th->prefetch_streams;		/* Number of prefetch streams for this thread. 	*/

    if (current_rule->testcase_type != PREFETCH_ONLY) {
        /* Set Thread Cancel Type as ASYNCHRONOUS */
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
    }

    #ifdef __HTX_LINUX__
       /*printf(" Prefetch:calling htx_bind with pcpu=%d for thread_no= %d\n",pcpu,thread_no);*/
    if(pcpu == -1){
        pcpu = htx_bind_thread(tid, -1);
        rc = pcpu;
        pcpus_thread_wise[thread_no]=pcpu;
        if(pcpu < 0){
            pcpus_thread_wise[thread_no]= -1;
        }

    }
    else {
        rc = htx_bind_thread(tid,pcpu);
    }
    #else
        rc = bindprocessor(BINDTHREAD, thread_self(), tid);
    #endif
		DEBUG_LOG("[%d] thread %d, binding to cpu %d \n",__LINE__,thread_no,tid);
	    
		if(rc < 0) {
	#ifdef __HTX_LINUX__
            if( rc == -2) {
				tot_thread_count --;
                sprintf(msg,"lcpu:%d(pcpu=%d) prefetch has been hot removed, thread will be terminating now tot_thread_count=%d\n",tid,pcpu,tot_thread_count);
                hxfmsg(&h_d, errno, HTX_HE_INFO, msg);
                pthread_exit(NULL);
            }
            else {
                sprintf(msg, "%d: Bindprocessor for prefetch irritator on lcpu:%d and corresponding pcpu:%d failed with rc=%d\n", __LINE__, tid,pcpu,rc);
                hxfmsg(&h_d, errno, HTX_HE_HARD_ERROR, msg);
            }

		#else
        sprintf(msg, "Binding to cpu:%d  failed with errno: %d \n",tid,errno);
        hxfmsg(&h_d, errno, HTX_HE_SOFT_ERROR, msg);

		#endif

        } /* End of if */
        else {
			/*sprintf(msg,"::physical cpu:%d for log cpu:%d\n",pcpu,tid);
            hxfmsg(&h_d, rc , HTX_HE_INFO, msg);*/
            #ifdef DEBUG
            sprintf(msg,"[%d] Bindprocessor success [prefetch thread_bo %d]! cpu_no : %d , pthread id : 0x%x \n",__LINE__,thread_no,tid,ptid);
            hxfmsg(&h_d, errno, HTX_HE_INFO, msg);
            #endif
        }

        th->seedval = time(NULL);
        srand48_r(th->seedval,&th->buffer);

       number_of_operations = current_rule->num_oper;

		starting_address = (unsigned long long)(th_array[thread_no].start_of_contiguous_memory);
		memory_fetch_size = current_rule->prefetch_memory_size - BYTES_EXC ;
		loop_count	= memory_fetch_size / cache_line_size;
       for (oper = 0; oper < number_of_operations ; oper++) {

        /* if SIGTERM was received, exit */
        if(exit_flag != 0) {
            break;
        }

        random_no = get_random_number_perf(thread_no);
        random_no = (unsigned long long)(random_no<<32) | (random_no);
		/*random_no = 0xaabbccdd;
		random_no = th_array[thread_no].random_pattern;*/
        th_array[thread_no].prev_seed = random_no;

		/* Now write DSCR if needed */
		if ( system_information.pvr >= POWER8_MURANO ) {
			prefetch_randomise_dscr(random_no, th->current_rule->pf_dscr , thread_no);
		}

        if (th_array[thread_no].prefetch_algorithm == RR_ALL_ENABLED_PREFETCH_ALGORITHMS) {
            /* Run all the enabled prefetch variants in round robin method */

            /* If prefetch nstride is set in the current prefetch configuration */
            if ( (PREFETCH_NSTRIDE & current_rule->pf_conf) == PREFETCH_NSTRIDE ) {
                n_stride(starting_address,memory_fetch_size,random_no,&th_array[thread_no].prefetch_scratch_mem[0]);
            }

        	/* if SIGTERM was received, exit */
            if (exit_flag != 0) {
                break;
            }

            /* If prefetch partial is set in the current prefetch configuration */
            if ( (PREFETCH_PARTIAL & current_rule->pf_conf) == PREFETCH_PARTIAL ) {
                partial_dcbt(starting_address,memory_fetch_size,random_no,&th_array[thread_no].prefetch_scratch_mem[0]);
            }

        	/* if SIGTERM was received, exit */
            if (exit_flag != 0) {
                break;
            }

            if ( (PREFETCH_IRRITATOR & current_rule->pf_conf) == PREFETCH_IRRITATOR ) {
				rc = do_prefetch( starting_address , memory_fetch_size , random_no, thread_no, loop_count, th_array[thread_no].pattern);
				if ( rc != 0 ) {
					sprintf(msg,"[%d] Miscompare in Prefetch!! Expected data = 0x%x Actual data = 0x%x thread_index : 0x%x Start of memory = %p, memory size = 0x%x\n"
								,__LINE__,th_array[thread_no].pattern, *(unsigned long long *)((unsigned char *)starting_address + 128*(loop_count-rc)), thread_no, starting_address, memory_fetch_size);
					hxfmsg(&h_d, 0, HTX_HE_MISCOMPARE, msg);
					dump_miscompare_data(thread_no, (unsigned char *)starting_address);
					return;
				}
                /*prefetch(starting_address,memory_fetch_size,random_no,&th_array[thread_no].prefetch_scratch_mem[0]);*/
            }

        	/* if SIGTERM was received, exit */
            if (exit_flag != 0) {
                break;
            }

            if( (PREFETCH_TRANSIENT & current_rule->pf_conf) == PREFETCH_TRANSIENT ) {
				/*lrand48_r(&th->buffer, &offset);*/
				offset		= random_no % (long)16;

				start_addr = (unsigned char *)starting_address + offset;
				rc = transient_dcbt((unsigned long long)start_addr, loop_count, th_array[thread_no].pattern );
				if ( rc != 0 ) {
					sprintf(msg,"[%d] Miscompare in Prefetch!! Expected data = 0x%x Actual data = 0x%x thread_index : 0x%x Start of memory = %p, memory size = 0x%x\n"
								,__LINE__,th_array[thread_no].pattern, *(unsigned long long *)((unsigned char *)starting_address + 128*(loop_count-rc)), thread_no, starting_address, memory_fetch_size);
					hxfmsg(&h_d, 0, HTX_HE_MISCOMPARE, msg);
					dump_miscompare_data(thread_no, (unsigned char *)starting_address);
					return;
				}
            }

        	/* if SIGTERM was received, exit */
            if (exit_flag != 0) {
                break;
            }

			if ( (PREFETCH_NA & current_rule->pf_conf) == PREFETCH_NA ) {

				/*lrand48_r(&th->buffer, &offset);*/
				offset		= random_no % (long)16;
				start_addr = (unsigned char *)starting_address + offset;
				rc = prefetch_dcbtna((unsigned long long)start_addr, loop_count, th_array[thread_no].pattern,&temp_storage,&temp_pattern);
				if ( rc != 0 ) {
					sprintf(msg,"[%d] Miscompare in Prefetch!! Expected data = 0x%x Actual data = 0x%x copied data = %x0x, copied pattern = %x0x, thread_index : 0x%x Start of memory = %p, memory size = 0x%x\n"
								,__LINE__,th_array[thread_no].pattern, *(unsigned long long *)((unsigned char *)starting_address + 128*(loop_count-rc)), temp_storage, temp_pattern, thread_no, starting_address, memory_fetch_size);
					hxfmsg(&h_d, 0, HTX_HE_MISCOMPARE, msg);
					dump_miscompare_data(thread_no, (unsigned char *)starting_address);
					return;
				}
			}
            if (exit_flag != 0) {
                break;
            }

        }
        else { /* Else Run only the specified algorithm */
            /*starting_address = (unsigned long long)(th_array[thread_no].start_of_contiguous_memory);
            memory_fetch_size = current_rule->prefetch_memory_size - BYTES_EXC ;*/

            if(th_array[thread_no].prefetch_algorithm == PREFETCH_NSTRIDE) {
				/*lrand48_r(&th->buffer, &random_no);*/
                n_stride(starting_address, memory_fetch_size, random_no, &th_array[thread_no].prefetch_scratch_mem[0]);
            }
            else if(th_array[thread_no].prefetch_algorithm == PREFETCH_PARTIAL) {
                partial_dcbt(starting_address, memory_fetch_size, random_no, &th_array[thread_no].prefetch_scratch_mem[0]);
            }
            else if(th_array[thread_no].prefetch_algorithm == PREFETCH_TRANSIENT) {
				/*lrand48_r(&th->buffer, &offset);*/
				offset		= random_no % (long)16;
				start_addr = (unsigned char *)starting_address + offset;

				rc = transient_dcbt((unsigned long long)start_addr, loop_count, th_array[thread_no].pattern );
				if ( rc != 0 ) {
					sprintf(msg,"[%d] Miscompare in Prefetch!! Expected data = 0x%x Actual data = 0x%x thread_index : 0x%x Start of memory = %p, memory size = 0x%x\n"
								,__LINE__,th_array[thread_no].pattern, *(unsigned long long *)((unsigned char *)starting_address + 128*(loop_count-rc)), thread_no, starting_address, memory_fetch_size);
					hxfmsg(&h_d, 0, HTX_HE_MISCOMPARE, msg);
					dump_miscompare_data(thread_no, (unsigned char *)starting_address);
					return;
				}
            }
            else if(th_array[thread_no].prefetch_algorithm == PREFETCH_IRRITATOR) {
				rc = do_prefetch( starting_address , memory_fetch_size , random_no, thread_no, loop_count, th_array[thread_no].pattern);
				if ( rc != 0 ) {
					sprintf(msg,"[%d] Miscompare in Prefetch!! Expected data = 0x%x Actual data = 0x%x thread_index : 0x%x Start of memory = %p, memory size = 0x%x\n"
								,__LINE__,th_array[thread_no].pattern, *(unsigned long long *)((unsigned char *)starting_address + 128*(loop_count-rc)), thread_no, starting_address, memory_fetch_size);
					hxfmsg(&h_d, 0, HTX_HE_MISCOMPARE, msg);
					dump_miscompare_data(thread_no, (unsigned char *)starting_address);
					return;
				}
            }
			else if ( th_array[thread_no].prefetch_algorithm == PREFETCH_NA ) {
				/*lrand48_r(&th->buffer, &offset);*/
				offset		= random_no % (long)16;
				start_addr = (unsigned char *)starting_address + offset;
				rc = prefetch_dcbtna((unsigned long long)start_addr, loop_count, th_array[thread_no].pattern,&temp_storage, &temp_pattern);
				if ( rc != 0 ) {
					sprintf(msg,"[%d] Miscompare in Prefetch ( returned %d)!! Expected data = 0x%x Actual data = 0x%x copied data = 0x%x, copied pattern = 0x%x, thread_index : 0x%x Start of memory = %p, offset = %d\n"
								,__LINE__, rc, th_array[thread_no].pattern, *(unsigned long long *)((unsigned char *)start_addr + 128*(loop_count-rc)), temp_storage, temp_pattern, thread_no, starting_address, offset);
					hxfmsg(&h_d, 0, HTX_HE_MISCOMPARE, msg);
					dump_miscompare_data(thread_no, (unsigned char *)starting_address);
					return;
				}
			}

        	/* if SIGTERM was received, exit */
            if(exit_flag != 0) {
                break;
            }
        }

    } /* End of for loop */
	#ifdef __HTX_LINUX__
        /* Restore original/default CPU affinity so that it binds to ANY available processor */

        rc = htx_unbind_thread();
	#else
        rc = bindprocessor(BINDTHREAD, thread_self(), PROCESSOR_CLASS_ANY);
	#endif
        if(rc == -1) {
                sprintf(msg, "%d: Unbinding from cpu:%d failed with errno %d \n",__LINE__, tid, errno);
                hxfmsg(&h_d, errno, HTX_HE_SOFT_ERROR, msg);
        }
	
#if defined(__HTX_MAMBO__) || defined(AWAN)
	printf("[%d] Thread no: %d, completed passes : %d\n",__LINE__, thread_no, oper);
#endif
}
Esempio n. 23
0
void initializeThread()
{
	srand((unsigned) getpid());

#if defined (_AIX)
	bool bindtosinglecpu = false;

	// kind of obsolete, do not set

	char* env = getenv("JLJBindToSingleCPU");
	if (0 != env)
	{
		int value = atoi(env);
		bindtosinglecpu = 1 == value;
	}

	if (bindtosinglecpu)
	{
		const int CPUCOUNT = sysconf(_SC_NPROCESSORS_ONLN);

		if (1 < CPUCOUNT)
		{
			const int CPU = rand() % CPUCOUNT;

			fprintf(
				stdout, 
				"\n__initbindprocessor() : trying to bind to cpu %d of %d ...",
				CPU,
				CPUCOUNT);

			int result = bindprocessor(BINDPROCESS, getpid(), CPU);

			if (0 == result)
			{
				fprintf(
					stdout,
					"\n__initbindprocessor() : successfully bound, affinity set to cpu %d.",
					CPU + 1);
			}
			else
			{
				fprintf(
					stdout,
					"\n__initbindprocessor() : failed, error = %d.",
					result);
			}

			fflush(stdout);
		}
	}

	env = getenv("AIXTHREAD_SCOPE");
	bool SYSTEMSCOPE = false;
	if (0 != env)
	{
		SYSTEMSCOPE = 0 == strcmp("S", env);
	}

	if (!SYSTEMSCOPE)
	{
		fprintf(stdout, "If jlj::lang::Thread is used it is strongly recommended to 'export AIXTHREAD_SCOPE=S' \nor application behaviour may be undefined.\n");
	}

#elif defined (__sun__)

//********************************************************************

#elif defined (__hpux)

//********************************************************************

#elif defined (_WINDOWS_SOURCE)

//********************************************************************

#endif
}