Monero
performance_utils.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30 
31 #pragma once
32 
33 #include <iostream>
34 
35 #include <boost/config.hpp>
36 
37 #ifdef BOOST_WINDOWS
38 #include <windows.h>
39 #endif
40 
41 void set_process_affinity(int core)
42 {
43 #if defined (__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__sun)
44  return;
45 #elif defined(BOOST_WINDOWS)
46  DWORD_PTR mask = 1;
47  for (int i = 0; i < core; ++i)
48  {
49  mask <<= 1;
50  }
51  ::SetProcessAffinityMask(::GetCurrentProcess(), core);
52 #elif defined(BOOST_HAS_PTHREADS)
53  cpu_set_t cpuset;
54  CPU_ZERO(&cpuset);
55  CPU_SET(core, &cpuset);
56  if (0 != ::pthread_setaffinity_np(::pthread_self(), sizeof(cpuset), &cpuset))
57  {
58  std::cout << "pthread_setaffinity_np - ERROR" << std::endl;
59  }
60 #endif
61 }
62 
64 {
65 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(_NetBSD_) || defined(__sun)
66  return;
67 #elif defined(BOOST_WINDOWS)
68  ::SetPriorityClass(::GetCurrentProcess(), HIGH_PRIORITY_CLASS);
69 #elif defined(BOOST_HAS_PTHREADS)
70  pthread_attr_t attr;
71  int policy = 0;
72  int max_prio_for_policy = 0;
73 
74  ::pthread_attr_init(&attr);
75  ::pthread_attr_getschedpolicy(&attr, &policy);
76  max_prio_for_policy = ::sched_get_priority_max(policy);
77 
78  if (0 != ::pthread_setschedprio(::pthread_self(), max_prio_for_policy))
79  {
80  std::cout << "pthread_setschedprio - ERROR" << std::endl;
81  }
82 
83  ::pthread_attr_destroy(&attr);
84 #endif
85 }
int i
Definition: pymoduletest.py:23
void set_process_affinity(int core)
Definition: performance_utils.h:41
void set_thread_high_priority()
Definition: performance_utils.h:63