/** * \brief Set the thread options (cpu affinitythread) * Priority should be already set by pthread_create * \param tv pointer to the ThreadVars of the calling thread */ TmEcode TmThreadSetupOptions(ThreadVars *tv) { if (tv->thread_setup_flags & THREAD_SET_AFFINITY) { SCLogInfo("Setting affinity for \"%s\" Module to cpu/core %"PRIu16", thread id %lu", tv->name, tv->cpu_affinity, SCGetThreadIdLong()); SetCPUAffinity(tv->cpu_affinity); } #if !defined OS_WIN32 && !defined __OpenBSD__ if (tv->thread_setup_flags & THREAD_SET_PRIORITY) TmThreadSetPrio(tv); if (tv->thread_setup_flags & THREAD_SET_AFFTYPE) { ThreadsAffinityType *taf = &thread_affinity[tv->cpu_affinity]; if (taf->mode_flag == EXCLUSIVE_AFFINITY) { int cpu = AffinityGetNextCPU(taf); SetCPUAffinity(cpu); /* If CPU is in a set overwrite the default thread prio */ if (CPU_ISSET(cpu, &taf->lowprio_cpu)) { tv->thread_priority = PRIO_LOW; } else if (CPU_ISSET(cpu, &taf->medprio_cpu)) { tv->thread_priority = PRIO_MEDIUM; } else if (CPU_ISSET(cpu, &taf->hiprio_cpu)) { tv->thread_priority = PRIO_HIGH; } else { tv->thread_priority = taf->prio; } SCLogInfo("Setting prio %d for \"%s\" Module to cpu/core %"PRIu16", thread id %lu", tv->thread_priority, tv->name, cpu, SCGetThreadIdLong()); } else { SetCPUAffinitySet(&taf->cpu_set); tv->thread_priority = taf->prio; } TmThreadSetPrio(tv); } #endif return TM_ECODE_OK; }
/** * \brief Set the thread affinity on the calling thread * \param cpuid id of the core/cpu to setup the affinity * \retval 0 if all goes well; -1 if something is wrong */ static int SetCPUAffinity(uint16_t cpuid) { #if !defined __OpenBSD__ int cpu = (int)cpuid; #endif #ifdef OS_WIN32 DWORD cs = 1 << cpu; #elif defined __OpenBSD__ return 0; #else cpu_set_t cs; CPU_ZERO(&cs); CPU_SET(cpu,&cs); #endif /* OS_WIN32 */ #ifdef OS_WIN32 int r = (0 == SetThreadAffinityMask(GetCurrentThread(), cs)); if (r != 0) { printf("Warning: sched_setaffinity failed (%" PRId32 "): %s\n", r, strerror(errno)); return -1; } SCLogDebug("CPU Affinity for thread %lu set to CPU %" PRId32, SCGetThreadIdLong(), cpu); return 0; #elif !defined __OpenBSD__ return SetCPUAffinitySet(&cs); #endif /* OS_WIN32 */ }
/** \internal * \brief fill lua stack with thread info * \param luastate the lua state * \param pa pointer to packet alert struct * \retval cnt number of data items placed on the stack * * Places: thread id (number), thread name (string, thread group name (string) */ static int LuaCallbackThreadInfoPushToStackFromThreadVars(lua_State *luastate, const ThreadVars *tv) { u_long tid = SCGetThreadIdLong(); lua_pushinteger (luastate, (lua_Integer)tid); lua_pushstring (luastate, tv->name); lua_pushstring (luastate, tv->thread_group_name); return 3; }
static int SetCPUAffinitySet(cpu_set_t *cs) { #if defined OS_FREEBSD int r = cpuset_setaffinity(CPU_LEVEL_WHICH,CPU_WHICH_TID,SCGetThreadIdLong(),sizeof(cpu_set_t),cs); #elif OS_DARWIN int r = thread_policy_set(mach_thread_self(), THREAD_AFFINITY_POLICY, (void*)cs, THREAD_AFFINITY_POLICY_COUNT); #else pid_t tid = syscall(SYS_gettid); int r = sched_setaffinity(tid,sizeof(cpu_set_t),cs); #endif /* OS_FREEBSD */ if (r != 0) { printf("Warning: sched_setaffinity failed (%" PRId32 "): %s\n", r, strerror(errno)); return -1; } return 0; }