1010#include <ctype.h>
1111#include <limits.h>
1212#include "internal.h"
13+ #include <api/fs/fs.h>
14+
15+ #define MAX_NR_CPUS 4096
1316
1417void perf_cpu_map__set_nr (struct perf_cpu_map * map , int nr_cpus )
1518{
@@ -100,12 +103,12 @@ static struct perf_cpu_map *cpu_map__new_sysconf(void)
100103static struct perf_cpu_map * cpu_map__new_sysfs_online (void )
101104{
102105 struct perf_cpu_map * cpus = NULL ;
103- FILE * onlnf ;
106+ char * buf = NULL ;
107+ size_t buf_len ;
104108
105- onlnf = fopen ("/sys/devices/system/cpu/online" , "r" );
106- if (onlnf ) {
107- cpus = perf_cpu_map__read (onlnf );
108- fclose (onlnf );
109+ if (sysfs__read_str ("devices/system/cpu/online" , & buf , & buf_len ) >= 0 ) {
110+ cpus = perf_cpu_map__new (buf );
111+ free (buf );
109112 }
110113 return cpus ;
111114}
@@ -158,62 +161,6 @@ static struct perf_cpu_map *cpu_map__trim_new(int nr_cpus, const struct perf_cpu
158161 return cpus ;
159162}
160163
161- struct perf_cpu_map * perf_cpu_map__read (FILE * file )
162- {
163- struct perf_cpu_map * cpus = NULL ;
164- int nr_cpus = 0 ;
165- struct perf_cpu * tmp_cpus = NULL , * tmp ;
166- int max_entries = 0 ;
167- int n , cpu , prev ;
168- char sep ;
169-
170- sep = 0 ;
171- prev = -1 ;
172- for (;;) {
173- n = fscanf (file , "%u%c" , & cpu , & sep );
174- if (n <= 0 )
175- break ;
176- if (prev >= 0 ) {
177- int new_max = nr_cpus + cpu - prev - 1 ;
178-
179- WARN_ONCE (new_max >= MAX_NR_CPUS , "Perf can support %d CPUs. "
180- "Consider raising MAX_NR_CPUS\n" , MAX_NR_CPUS );
181-
182- if (new_max >= max_entries ) {
183- max_entries = new_max + MAX_NR_CPUS / 2 ;
184- tmp = realloc (tmp_cpus , max_entries * sizeof (struct perf_cpu ));
185- if (tmp == NULL )
186- goto out_free_tmp ;
187- tmp_cpus = tmp ;
188- }
189-
190- while (++ prev < cpu )
191- tmp_cpus [nr_cpus ++ ].cpu = prev ;
192- }
193- if (nr_cpus == max_entries ) {
194- max_entries += MAX_NR_CPUS ;
195- tmp = realloc (tmp_cpus , max_entries * sizeof (struct perf_cpu ));
196- if (tmp == NULL )
197- goto out_free_tmp ;
198- tmp_cpus = tmp ;
199- }
200-
201- tmp_cpus [nr_cpus ++ ].cpu = cpu ;
202- if (n == 2 && sep == '-' )
203- prev = cpu ;
204- else
205- prev = -1 ;
206- if (n == 1 || sep == '\n' )
207- break ;
208- }
209-
210- if (nr_cpus > 0 )
211- cpus = cpu_map__trim_new (nr_cpus , tmp_cpus );
212- out_free_tmp :
213- free (tmp_cpus );
214- return cpus ;
215- }
216-
217164struct perf_cpu_map * perf_cpu_map__new (const char * cpu_list )
218165{
219166 struct perf_cpu_map * cpus = NULL ;
@@ -238,15 +185,15 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
238185 p = NULL ;
239186 start_cpu = strtoul (cpu_list , & p , 0 );
240187 if (start_cpu >= INT_MAX
241- || (* p != '\0' && * p != ',' && * p != '-' ))
188+ || (* p != '\0' && * p != ',' && * p != '-' && * p != '\n' ))
242189 goto invalid ;
243190
244191 if (* p == '-' ) {
245192 cpu_list = ++ p ;
246193 p = NULL ;
247194 end_cpu = strtoul (cpu_list , & p , 0 );
248195
249- if (end_cpu >= INT_MAX || (* p != '\0' && * p != ',' ))
196+ if (end_cpu >= INT_MAX || (* p != '\0' && * p != ',' && * p != '\n' ))
250197 goto invalid ;
251198
252199 if (end_cpu < start_cpu )
@@ -265,7 +212,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
265212 goto invalid ;
266213
267214 if (nr_cpus == max_entries ) {
268- max_entries += MAX_NR_CPUS ;
215+ max_entries += max ( end_cpu - start_cpu + 1 , 16UL ) ;
269216 tmp = realloc (tmp_cpus , max_entries * sizeof (struct perf_cpu ));
270217 if (tmp == NULL )
271218 goto invalid ;
@@ -279,14 +226,15 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
279226 cpu_list = p ;
280227 }
281228
282- if (nr_cpus > 0 )
229+ if (nr_cpus > 0 ) {
283230 cpus = cpu_map__trim_new (nr_cpus , tmp_cpus );
284- else if (* cpu_list != '\0' ) {
231+ } else if (* cpu_list != '\0' ) {
285232 pr_warning ("Unexpected characters at end of cpu list ('%s'), using online CPUs." ,
286233 cpu_list );
287234 cpus = perf_cpu_map__new_online_cpus ();
288- } else
235+ } else {
289236 cpus = perf_cpu_map__new_any_cpu ();
237+ }
290238invalid :
291239 free (tmp_cpus );
292240out :
0 commit comments