File: info.c

package info (click to toggle)
libspe2 2.2.80-95-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 940 kB
  • ctags: 1,214
  • sloc: ansic: 9,316; makefile: 551; ada: 448; sh: 24
file content (124 lines) | stat: -rw-r--r-- 3,019 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * libspe2 - A wrapper library to adapt the JSRE SPU usage model to SPUFS
 * Copyright (C) 2005 IBM Corp.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License,
 * or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <dirent.h>
#include <errno.h>
#include <stdio.h>


#include "info.h"

/*
 * For the moment, we count the numbers of cpus and devide by 2.
 */ 
int _base_spe_count_physical_cpus(int cpu_node)
{
	const char    *buff = "/sys/devices/system/cpu";
	DIR     *dirp;
	int ret = -2;
	struct  dirent  *dptr;

	DEBUG_PRINTF ("spe_count_physical_cpus()\n");
	
	// make sure, cpu_node is in the correct range
	if (cpu_node != -1) {
		errno = EINVAL;
		return -1;
	}

	// Count number of CPUs in /sys/devices/system/cpu
	if((dirp=opendir(buff))==NULL) {
		fprintf(stderr,"Error opening %s ",buff);
		perror("dirlist");
		errno = EINVAL;
		return -1;
	}
    while((dptr=readdir(dirp))) {
		ret++;
	}
	closedir(dirp);
	return ret/THREADS_PER_BE; 
}

/*
 * For the moment, we use all spes which are controlled by linux
 */
int _base_spe_count_usable_spes(int cpu_node)
{
	return _base_spe_count_physical_spes(cpu_node); // FIXME
}

/*
 * For the moment, we assume all SPEs are evenly distributed over the 
 * physical cpsus.
 */
int _base_spe_count_physical_spes(int cpu_node)
{
	const char	*buff = "/sys/devices/system/spu";
	DIR	*dirp;
	int ret = -2;
	struct	dirent	*dptr;
	int no_of_bes;

	DEBUG_PRINTF ("spe_count_physical_spes()\n");

	// make sure, cpu_node is in the correct range
	no_of_bes = _base_spe_count_physical_cpus(-1);
	if (cpu_node < -1 || cpu_node >= no_of_bes ) {
		errno = EINVAL;
		return -1;
	}

	// Count number of SPUs in /sys/devices/system/spu
	if((dirp=opendir(buff))==NULL) {
		fprintf(stderr,"Error opening %s ",buff);
		perror("dirlist");
		errno = EINVAL;
		return -1;
	}
    while((dptr=readdir(dirp))) {
		ret++;
	}
	closedir(dirp);

	if(cpu_node != -1) ret /= no_of_bes; // FIXME
	return ret;
}


int _base_spe_cpu_info_get(int info_requested, int cpu_node) {
	int ret = 0;
	errno = 0;
	
	switch (info_requested) {
	case  SPE_COUNT_PHYSICAL_CPU_NODES:
		ret = _base_spe_count_physical_cpus(cpu_node);
		break;
	case SPE_COUNT_PHYSICAL_SPES:
		ret = _base_spe_count_physical_spes(cpu_node);
		break;
	case SPE_COUNT_USABLE_SPES:
		ret = _base_spe_count_usable_spes(cpu_node);
		break;
	default:
		errno = EINVAL;
		ret = -1;
	}
	return ret;
}