From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12321 invoked by alias); 20 Nov 2012 06:13:48 -0000 Received: (qmail 12074 invoked by uid 22791); 20 Nov 2012 06:13:47 -0000 X-SWARE-Spam-Status: No, hits=-3.1 required=5.0 tests=AWL,BAYES_00,KHOP_THREADED X-Spam-Check-By: sourceware.org Received: from shards.monkeyblade.net (HELO shards.monkeyblade.net) (149.20.54.216) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 20 Nov 2012 06:13:42 +0000 Received: from localhost (cpe-66-108-117-132.nyc.res.rr.com [66.108.117.132]) by shards.monkeyblade.net (Postfix) with ESMTPSA id B358B583348; Mon, 19 Nov 2012 22:13:44 -0800 (PST) Date: Tue, 20 Nov 2012 06:13:00 -0000 Message-Id: <20121120.011339.2105774812993461488.davem@davemloft.net> To: gdb-patches@sourceware.org Cc: stan@codesourcery.com, kcy@codesourcery.com Subject: Re: [PATCH] Fix 'info os' crashes on sparc. From: David Miller In-Reply-To: <20121120.003006.1287326526424655175.davem@davemloft.net> References: <20121120.003006.1287326526424655175.davem@davemloft.net> Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2012-11/txt/msg00528.txt.bz2 From: David Miller Date: Tue, 20 Nov 2012 00:30:06 -0500 (EST) > 2012-11-19 David S. Miller > > * common/linux-osdata.c (get_number_of_cpu_cores): Add support > for parsing the layout of sparc /proc/cpuinfo files. Actually, there is a better fix, GLIBC already has all of this /proc parsing logic for every cpu type, and can even distinguish activated vs. non-activated cpus. We simply need to do sysconf(_SC_NPROCESSORS_ONLN) The thing is, this code still has a problem. The number of cpus active doesn't tell us anything about what the largest cpu number might be. The cpus numbers themselves can be arbitrarily sparse and beyond the number of cpus. And it's the cpu numbers that are used to index these various tables. gdb/ 2012-11-19 David S. Miller * common/linux-osdata.c (get_number_of_cpu_cores): Delete. (linux_xfer_osdata_processes): Fetch _SC_NPROCESSORS_ONLN via sysconf. (get_cores_used_by_process): Update comment. diff --git a/gdb/common/linux-osdata.c b/gdb/common/linux-osdata.c index afe3e75..d54f9d3 100644 --- a/gdb/common/linux-osdata.c +++ b/gdb/common/linux-osdata.c @@ -26,6 +26,7 @@ #include "linux-osdata.h" #include +#include #include #include #include @@ -253,30 +254,8 @@ get_process_owner (uid_t *owner, PID_T pid) return -1; } -/* Returns the number of CPU cores found on the system. */ - -static int -get_number_of_cpu_cores (void) -{ - int cores = 0; - FILE *f = fopen ("/proc/cpuinfo", "r"); - - while (!feof (f)) - { - char buf[512]; - char *p = fgets (buf, sizeof (buf), f); - - if (p && strncmp (buf, "processor", 9) == 0) - ++cores; - } - - fclose (f); - - return cores; -} - /* Find the CPU cores used by process PID and return them in CORES. - CORES points to an array of at least get_number_of_cpu_cores () + CORES points to an array of at least sysconf(_SC_NPROCESSOR_ONLN) elements. */ static int @@ -340,7 +319,7 @@ linux_xfer_osdata_processes (gdb_byte *readbuf, dirp = opendir ("/proc"); if (dirp) { - const int num_cores = get_number_of_cpu_cores (); + const int num_cores = sysconf (_SC_NPROCESSORS_ONLN); struct dirent *dp; while ((dp = readdir (dirp)) != NULL)