Changeset 557 for trunk/lib


Ignore:
Timestamp:
Jul 30, 2010, 8:06:35 PM (9 years ago)
Author:
ksherdy
Message:

Added functionality to programmatically bind a process to a core.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/perflib/perfsec.h

    r517 r557  
    1010        #include "../cclib/cc.cxx"     
    1111#endif
     12
     13#if defined __GNUC__
     14
     15  #include <sched.h>
     16
     17  int set_processor_affinity(unsigned long mask) { // mask = 1 sets hard processor affinity for core 1, mask = 2 for core 2, mask = 4 for core 3, mask = 7 for core 1 and core 2 and core 3.
     18
     19    unsigned int len = sizeof(mask);
     20
     21    if (sched_setaffinity(0, len, (const cpu_set_t *)&mask) < 0) {
     22      printf("sched_setaffinity call failed.\n");
     23      return -1;
     24    }
     25
     26    printf("CPU affinity mask: %08lx\n", mask);
     27
     28    return 0;
     29  }
     30
     31#elif defined _MSC_VER
     32
     33  #include <process.h>
     34  #include <Windows.h>
     35  #define getpid _getpid
     36
     37  int set_processor_affinity(DWORD_PTR mask) {
     38
     39    HANDLE hProcess  = OpenProcess(PROCESS_ALL_ACCESS, 0, getpid());
     40
     41        if(0 == SetProcessAffinityMask(hProcess, mask)) {
     42              printf("SetProcessAffinityMask call failed.\n");
     43              return -1;
     44        }
     45
     46        printf("CPU affinity mask: %08lx\n", mask);
     47
     48        return 0;
     49  }
     50
     51#else
     52
     53  int set_processor_affinity(unsigned long mask) {
     54        printf("set_processor_affinity is not implemented on the current OS.\n");
     55        return -1;
     56  }
     57
     58#endif
     59
     60#define PERF_SEC_BIND(mask) \
     61        set_processor_affinity(mask)
    1262
    1363#if defined(WALL_TIMING)
Note: See TracChangeset for help on using the changeset viewer.