MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
task2cpu.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbutil/task2cpu.cc,v 1.23 2017/01/12 14:44:05 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 #include "ac/pthread.h"
35 
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #ifdef HAVE_TASK2CPU
40 #include <sys/ioctl.h>
41 #endif // HAVE_TASK2CPU
42 #include <iostream>
43 
44 #ifdef USE_PTHREAD_SETAFFINITY_NP
45 #include <sched.h>
46 #endif
47 
48 #include "myassert.h"
49 
50 static bool mbdyn_task2cpu_disabled = false;
51 #ifdef HAVE_THREADS
52 static pthread_mutex_t mbdyn_task2cpu_mutex = PTHREAD_MUTEX_INITIALIZER;
53 #endif /* HAVE_THREADS */
54 
55 int
57 {
58  int fd = -1;
59 
60 #ifdef HAVE_THREADS
61  pthread_mutex_lock(&::mbdyn_task2cpu_mutex);
62 #endif /* HAVE_THREADS */
63  if (!::mbdyn_task2cpu_disabled) {
64 #ifdef HAVE_TASK2CPU
65  fd = open("/dev/TASK2CPU", O_RDWR);
66  if (fd != -1) {
67  ioctl(fd, 0, cpu);
68  close(fd);
69 
70  } else {
71  int save_errno = errno;
72  char *err_msg = strerror(save_errno);
73 
74  silent_cerr("Error opening /dev/TASK2CPU ("
75  << save_errno << ": " << err_msg << ";"
76  " ignored)" << std::endl);
78  }
79 #elif defined(USE_PTHREAD_SETAFFINITY_NP)
80  cpu_set_t cpuset;
81  CPU_ZERO(&cpuset);
82  CPU_SET(cpu, &cpuset);
83 
84  const pthread_t thread = pthread_self();
85 
86  int s = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset);
87 
88  if (0 != s) {
89  silent_cerr("warning: pthread_setaffinity_np failed with status " << s << std::endl);
90  goto failed;
91  }
92 
93 #ifdef DEBUG
94  s = pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset);
95 
96  if (0 != s) {
97  DEBUGCERR("warning: pthread_getaffinity_np failed with status " << s << std::endl);
98  goto failed;
99  }
100 
101  for (int i = 0; i < CPU_SETSIZE; ++i) {
102  if (CPU_ISSET(i, &cpuset)) {
103  DEBUGCERR("thread " << cpu + 1 << " running at CPU " << i << std::endl);
104  }
105  }
106 #endif
107  fd = 1; // return 0
108 
109  failed:
110  if (fd == -1) {
112  }
113 #else /* ! HAVE_TASK2CPU */
114  silent_cerr("/dev/TASK2CPU or pthread_getaffinity_np are not available" << std::endl);
116 #endif /* ! HAVE_TASK2CPU */
117  }
118 #ifdef HAVE_THREADS
119  pthread_mutex_unlock(&::mbdyn_task2cpu_mutex);
120 #endif /* HAVE_THREADS */
121 
122  return (fd == -1);
123 }
124 
#define DEBUGCERR(msg)
Definition: myassert.h:235
int mbdyn_task2cpu(int cpu)
Definition: task2cpu.cc:56
static bool mbdyn_task2cpu_disabled
Definition: task2cpu.cc:50