/* * Copyright 2011 Samy Al Bahra. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #ifdef __linux__ #include #include #include #include #endif #ifndef CORES #define CORES 8 #endif struct affinity { unsigned int delta; unsigned int request; }; #define AFFINITY_INITIALIZER {0, 0} #ifdef __linux__ #ifndef gettid static pid_t gettid(void) { return syscall(__NR_gettid); } #endif /* gettid */ CK_CC_UNUSED static int aff_iterate(struct affinity *acb) { cpu_set_t s; unsigned int c; c = ck_pr_faa_uint(&acb->request, acb->delta); CPU_ZERO(&s); CPU_SET(c % CORES, &s); return sched_setaffinity(gettid(), sizeof(s), &s); } #else CK_CC_UNUSED static int aff_iterate(struct affinity *acb CK_CC_UNUSED) { return (0); } #endif CK_CC_INLINE static uint64_t rdtsc(void) { #if defined(__x86_64__) uint32_t eax = 0, edx; __asm__ __volatile__("cpuid;" "rdtsc;" : "+a" (eax), "=d" (edx) : : "%ecx", "%ebx", "memory"); __asm__ __volatile__("xorl %%eax, %%eax;" "cpuid;" : : : "%eax", "%ebx", "%ecx", "%edx", "memory"); return (((uint64_t)edx << 32) | eax); #elif defined(__sparcv9__) uint64_t r; __asm__ __volatile__("rd %%tick, %0" : "=r" (r) :: "memory"); return r; #else return 0; #endif }