libspe2  0.9a
info.c
Go to the documentation of this file.
1 /*
2  * libspe2 - A wrapper library to adapt the JSRE SPU usage model to SPUFS
3  * Copyright (C) 2005 IBM Corp.
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation; either version 2.1 of the License,
8  * or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13  * License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #include <dirent.h>
21 #include <errno.h>
22 #include <stdio.h>
23 
24 
25 #include "info.h"
26 
27 /*
28  * For the moment, we count the numbers of cpus and devide by 2.
29  */
31 {
32  const char *buff = "/sys/devices/system/cpu";
33  DIR *dirp;
34  int ret = -2;
35  struct dirent *dptr;
36 
37  DEBUG_PRINTF ("spe_count_physical_cpus()\n");
38 
39  // make sure, cpu_node is in the correct range
40  if (cpu_node != -1) {
41  errno = EINVAL;
42  return -1;
43  }
44 
45  // Count number of CPUs in /sys/devices/system/cpu
46  if((dirp=opendir(buff))==NULL) {
47  fprintf(stderr,"Error opening %s ",buff);
48  perror("dirlist");
49  errno = EINVAL;
50  return -1;
51  }
52  while((dptr=readdir(dirp))) {
53  ret++;
54  }
55  closedir(dirp);
56  return ret/THREADS_PER_BE;
57 }
58 
59 /*
60  * For the moment, we use all spes which are controlled by linux
61  */
62 int _base_spe_count_usable_spes(int cpu_node)
63 {
64  return _base_spe_count_physical_spes(cpu_node); // FIXME
65 }
66 
67 /*
68  * For the moment, we assume all SPEs are evenly distributed over the
69  * physical cpsus.
70  */
72 {
73  const char *buff = "/sys/devices/system/spu";
74  DIR *dirp;
75  int ret = -2;
76  struct dirent *dptr;
77  int no_of_bes;
78 
79  DEBUG_PRINTF ("spe_count_physical_spes()\n");
80 
81  // make sure, cpu_node is in the correct range
82  no_of_bes = _base_spe_count_physical_cpus(-1);
83  if (cpu_node < -1 || cpu_node >= no_of_bes ) {
84  errno = EINVAL;
85  return -1;
86  }
87 
88  // Count number of SPUs in /sys/devices/system/spu
89  if((dirp=opendir(buff))==NULL) {
90  fprintf(stderr,"Error opening %s ",buff);
91  perror("dirlist");
92  errno = EINVAL;
93  return -1;
94  }
95  while((dptr=readdir(dirp))) {
96  ret++;
97  }
98  closedir(dirp);
99 
100  if(cpu_node != -1) ret /= no_of_bes; // FIXME
101  return ret;
102 }
103 
104 
105 int _base_spe_cpu_info_get(int info_requested, int cpu_node) {
106  int ret = 0;
107  errno = 0;
108 
109  switch (info_requested) {
111  ret = _base_spe_count_physical_cpus(cpu_node);
112  break;
114  ret = _base_spe_count_physical_spes(cpu_node);
115  break;
117  ret = _base_spe_count_usable_spes(cpu_node);
118  break;
119  default:
120  errno = EINVAL;
121  ret = -1;
122  }
123  return ret;
124 }
#define THREADS_PER_BE
Definition: info.h:25
#define DEBUG_PRINTF(fmt, args...)
Definition: elf_loader.c:45
#define SPE_COUNT_PHYSICAL_CPU_NODES
int _base_spe_count_physical_spes(int cpu_node)
Definition: info.c:71
int _base_spe_count_physical_cpus(int cpu_node)
Definition: info.c:30
#define SPE_COUNT_PHYSICAL_SPES
int _base_spe_cpu_info_get(int info_requested, int cpu_node)
Definition: info.c:105
#define SPE_COUNT_USABLE_SPES
int _base_spe_count_usable_spes(int cpu_node)
Definition: info.c:62