roughly auto-config threads depending on cpu count

This commit is contained in:
Jean-Francois Dockes
2013-04-25 09:00:50 +02:00
parent 2b80c77c23
commit ec68cd95ba
11 changed files with 236 additions and 10 deletions
+6
View File
@@ -37,6 +37,12 @@ trreadfile.o : readfile.cpp readfile.h
$(CXX) -o trreadfile.o -c $(ALL_CXXFLAGS) \
-DTEST_READFILE readfile.cpp
CPUCONF_OBJS= trcpuconf.o
trcpuconf : $(CPUCONF_OBJS)
$(CXX) -o trcpuconf $(CPUCONF_OBJS) $(LIBRECOLL) $(LIBICONV) $(LIBSYS)
trcpuconf.o : cpuconf.cpp cpuconf.h
$(CXX) -o trcpuconf.o -c $(ALL_CXXFLAGS) -DTEST_CPUCONF cpuconf.cpp
CIRCACHE_OBJS= trcircache.o
trcircache : $(CIRCACHE_OBJS)
$(CXX) -o trcircache $(CIRCACHE_OBJS) \
+86
View File
@@ -0,0 +1,86 @@
/* Copyright (C) 2013 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef TEST_CPUCONF
#include "autoconfig.h"
#include "cpuconf.h"
#include "execmd.h"
#include "smallut.h"
#if defined(__gnu_linux__)
bool getCpuConf(CpuConf& conf)
{
vector<string> cmdv = create_vector<string>("sh")("-c")
("egrep ^processor /proc/cpuinfo | wc -l");
string result;
if (!ExecCmd::backtick(cmdv, result))
return false;
conf.ncpus = atoi(result.c_str());
if (conf.ncpus < 1 || conf.ncpus > 100)
conf.ncpus = 1;
return true;
}
#elif defined(__FreeBSD__)
bool getCpuConf(CpuConf& conf)
{
vector<string> cmdv = create_vector<string>("sysctl")("hw.ncpu");
string result;
if (!ExecCmd::backtick(cmdv, result))
return false;
conf.ncpus = atoi(result.c_str());
if (conf.ncpus < 1 || conf.ncpus > 100)
conf.ncpus = 1;
return true;
}
//#elif defined(__APPLE__)
#else // Any other system
// Generic, pretend there is one
bool getCpuConf(CpuConf& cpus)
{
cpu.cpus = 1;
return true;
}
#endif
#else // TEST_CPUCONF
#include <stdlib.h>
#include <iostream>
using namespace std;
#include "cpuconf.h"
// Test driver
int main(int argc, const char **argv)
{
CpuConf cpus;
if (!getCpuConf(cpus)) {
cerr << "getCpuConf failed" << endl;
exit(1);
}
cout << "Cpus: " << cpus.ncpus << endl;
exit(0);
}
#endif // TEST_CPUCONF
+34
View File
@@ -0,0 +1,34 @@
/* Copyright (C) 2013 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _CPUCONF_H_INCLUDED_
#define _CPUCONF_H_INCLUDED_
/** Guess how many CPUs there are on this machine, to help with configuring
threads */
struct CpuConf {
CpuConf()
: ncpus(1)
{}
// Virtual ones, including hyperthreading, we only care about this for now
int ncpus;
// int ncores;
// int nsockets;
};
extern bool getCpuConf(CpuConf& conf);
#endif /* _CPUCONF_H_INCLUDED_ */
+1 -1
View File
@@ -536,7 +536,7 @@ int ExecCmd::send(const string& data)
break;
int n = con->send(data.c_str() + nwritten, data.length() - nwritten);
if (n < 0) {
LOGERR(("ExecCmd::doexec: send failed\n"));
LOGERR(("ExecCmd::send: send failed\n"));
return -1;
}
nwritten += n;
+1 -1
View File
@@ -201,7 +201,7 @@ inline void leftzeropad(string& s, unsigned len)
// Code for static initialization of an stl map. Somewhat like Boost.assign.
// Ref: http://stackoverflow.com/questions/138600/initializing-a-static-stdmapint-int-in-c
// Example use: map<int, int> m = create_map (1,2) (3,4) (5,6) (7,8);
// Example use: map<int, int> m = create_map<int, int> (1,2) (3,4) (5,6) (7,8);
template <typename T, typename U>
class create_map