From 1f2b71e31326a5bc021785888cd2203010acf4e6 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Mon, 4 Jun 2018 19:58:34 +0200
Subject: Defined the number of threads to run from a centralized function.

---
 plugins/dex/pool.c                 |  7 ++---
 plugins/elf/symbols.c              |  5 ++--
 src/analysis/disass/area.c         |  7 ++---
 src/analysis/disass/disassembler.c |  5 ++--
 src/core/Makefile.am               |  1 +
 src/core/nproc.c                   | 53 ++++++++++++++++++++++++++++++++++++++
 src/core/nproc.h                   | 37 ++++++++++++++++++++++++++
 src/glibext/delayed.c              |  5 ++--
 src/glibext/gwidthtracker.c        |  3 ++-
 src/glibext/signal.c               |  2 +-
 10 files changed, 111 insertions(+), 14 deletions(-)
 create mode 100644 src/core/nproc.c
 create mode 100644 src/core/nproc.h

diff --git a/plugins/dex/pool.c b/plugins/dex/pool.c
index 1ed2c44..081c178 100644
--- a/plugins/dex/pool.c
+++ b/plugins/dex/pool.c
@@ -30,6 +30,7 @@
 
 #include <i18n.h>
 #include <core/global.h>
+#include <core/nproc.h>
 #include <mangling/demangler.h>
 
 
@@ -201,7 +202,7 @@ bool load_all_dex_types(GDexFormat *format, wgroup_id_t gid, GtkStatusStack *sta
 
     /* Lancement des chargements */
 
-    runs_count = g_get_num_processors();
+    runs_count = get_max_online_threads();
 
     run_size = count / runs_count;
 
@@ -362,7 +363,7 @@ bool load_all_dex_fields(GDexFormat *format, wgroup_id_t gid, GtkStatusStack *st
 
     /* Lancement des chargements */
 
-    runs_count = g_get_num_processors();
+    runs_count = get_max_online_threads();
 
     run_size = count / runs_count;
 
@@ -731,7 +732,7 @@ bool load_all_dex_classes(GDexFormat *format, wgroup_id_t gid, GtkStatusStack *s
 
     /* Lancement des chargements */
 
-    runs_count = g_get_num_processors();
+    runs_count = get_max_online_threads();
 
     run_size = format->header.class_defs_size / runs_count;
 
diff --git a/plugins/elf/symbols.c b/plugins/elf/symbols.c
index 1b47616..949e495 100644
--- a/plugins/elf/symbols.c
+++ b/plugins/elf/symbols.c
@@ -36,6 +36,7 @@
 #include <common/sort.h>
 #include <core/global.h>
 #include <core/logs.h>
+#include <core/nproc.h>
 #include <format/symiter.h>
 #include <mangling/demangler.h>
 
@@ -597,7 +598,7 @@ static void add_all_elf_symbols(GElfFormat *format, phys_t sym_start, size_t cou
 
     sym_size = ELF_SIZEOF_SYM(format);
 
-    runs_count = g_get_num_processors();
+    runs_count = get_max_online_threads();
 
     run_size = count / runs_count;
 
@@ -1013,7 +1014,7 @@ static bool load_elf_relocations(GElfFormat *format, const elf_phdr *dynamic, el
 
     queue = get_work_queue();
 
-    runs_count = g_get_num_processors();
+    runs_count = get_max_online_threads();
 
     run_size = length / runs_count;
 
diff --git a/src/analysis/disass/area.c b/src/analysis/disass/area.c
index 655d5af..c52934f 100644
--- a/src/analysis/disass/area.c
+++ b/src/analysis/disass/area.c
@@ -39,6 +39,7 @@
 #include "../../common/sort.h"
 #include "../../core/global.h"
 #include "../../core/logs.h"
+#include "../../core/nproc.h"
 #include "../../format/format.h"
 #include "../../glibext/delayed-int.h"
 
@@ -1907,7 +1908,7 @@ mem_area *collect_memory_areas(wgroup_id_t gid, GtkStatusStack *status, GLoadedB
 
     /* Lancement des traitements */
 
-    runs_count = g_get_num_processors();
+    runs_count = get_max_online_threads();
 
     collectors = (GAreaCollector **)calloc(runs_count, sizeof(GAreaCollector *));
 
@@ -2085,7 +2086,7 @@ void populate_fresh_memory_areas(wgroup_id_t gid, GtkStatusStack *status, mem_ar
 
     icount = _g_preload_info_count_instructions(info);
 
-    runs_count = g_get_num_processors();
+    runs_count = get_max_online_threads();
 
     collectors = (GAreaCollector **)calloc(runs_count, sizeof(GAreaCollector *));
 
@@ -2236,7 +2237,7 @@ GArchInstruction **collect_disassembled_instructions(wgroup_id_t gid, GtkStatusS
 
     /* Lancement des traitements */
 
-    runs_count = g_get_num_processors();
+    runs_count = get_max_online_threads();
 
     collectors = (GAreaCollector **)calloc(runs_count, sizeof(GAreaCollector *));
 
diff --git a/src/analysis/disass/disassembler.c b/src/analysis/disass/disassembler.c
index 0995b63..a88ba1e 100644
--- a/src/analysis/disass/disassembler.c
+++ b/src/analysis/disass/disassembler.c
@@ -38,6 +38,7 @@
 #include "routines.h"
 #include "../../arch/storage.h"
 #include "../../core/global.h"
+#include "../../core/nproc.h"
 #include "../../glibext/generators/prologue.h"
 #include "../../plugins/pglist.h"
 
@@ -88,7 +89,7 @@ static void process_all_instructions(wgroup_id_t gid, GtkStatusStack *status, co
     size_t end;                             /* Fin d'un bloc de traitement */
     GInstructionsStudy *study;              /* Tâche d'étude à programmer  */
 
-    runs_count = g_get_num_processors();
+    runs_count = get_max_online_threads();
 
     g_arch_processor_lock(proc);
 
@@ -160,7 +161,7 @@ static void process_all_routines(wgroup_id_t gid, GtkStatusStack *status, const
 
     sym_count = g_binary_format_count_symbols(format);
 
-    runs_count = g_get_num_processors();
+    runs_count = get_max_online_threads();
 
     run_size = sym_count / runs_count;
 
diff --git a/src/core/Makefile.am b/src/core/Makefile.am
index 7acadea..4dcf6e7 100644
--- a/src/core/Makefile.am
+++ b/src/core/Makefile.am
@@ -8,6 +8,7 @@ libcore_la_SOURCES =					\
 	formats.h formats.c					\
 	global.h global.c					\
 	logs.h logs.c						\
+	nproc.h nproc.c						\
 	params.h params.c					\
 	processors.h processors.c			\
 	queue.h queue.c
diff --git a/src/core/nproc.c b/src/core/nproc.c
new file mode 100644
index 0000000..65d6ec0
--- /dev/null
+++ b/src/core/nproc.c
@@ -0,0 +1,53 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * nproc.c - détermination du volume de traitements parallèles idéal
+ *
+ * Copyright (C) 2018 Cyrille Bagard
+ *
+ *  This file is part of Chrysalide.
+ *
+ *  Chrysalide 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 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  Chrysalide 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 Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "nproc.h"
+
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : -                                                            *
+*                                                                             *
+*  Description : Indique le nombre idéal de tâches pour bien occuper le CPU.  *
+*                                                                             *
+*  Retour      : Quantité de threads pour les groupes de travail sans retenue.*
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+guint get_max_online_threads(void)
+{
+    guint result;                           /* Estimation à retourner      */
+
+    result = g_get_num_processors();
+
+    /**
+     * Un peu arbitraire, mais bon...
+     */
+    result *= 2;
+
+    return result;
+
+}
diff --git a/src/core/nproc.h b/src/core/nproc.h
new file mode 100644
index 0000000..4a175b9
--- /dev/null
+++ b/src/core/nproc.h
@@ -0,0 +1,37 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * nproc.h - prototypes pour la détermination du volume de traitements parallèles idéal
+ *
+ * Copyright (C) 2018 Cyrille Bagard
+ *
+ *  This file is part of Chrysalide.
+ *
+ *  Chrysalide 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 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  Chrysalide 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 Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef _CORE_NPROC_H
+#define _CORE_NPROC_H
+
+
+#include <glib.h>
+
+
+
+/* Indique le nombre idéal de tâches pour bien occuper le CPU. */
+guint get_max_online_threads(void);
+
+
+
+#endif  /* _CORE_NPROC_H */
diff --git a/src/glibext/delayed.c b/src/glibext/delayed.c
index caffbaa..6e81c4b 100644
--- a/src/glibext/delayed.c
+++ b/src/glibext/delayed.c
@@ -32,6 +32,7 @@
 
 
 #include "delayed-int.h"
+#include "../core/nproc.h"
 #include "../gui/core/global.h"
 
 
@@ -396,7 +397,7 @@ static void g_work_group_init(GWorkGroup *group)
     g_atomic_int_set(&group->pending, 0);
 
     group->threads = NULL;
-    group->threads_count = g_get_num_processors();
+    group->threads_count = 0;
     group->force_exit = false;
 
     group->callback = NULL;
@@ -501,7 +502,7 @@ static GWorkGroup *g_work_group_new(wgroup_id_t id, const guint *count)
 
     result->id = id;
 
-    result->threads_count = g_get_num_processors();
+    result->threads_count = get_max_online_threads();
 
     if (count != NULL && *count < result->threads_count)
         result->threads_count = *count;
diff --git a/src/glibext/gwidthtracker.c b/src/glibext/gwidthtracker.c
index 181759c..0741133 100644
--- a/src/glibext/gwidthtracker.c
+++ b/src/glibext/gwidthtracker.c
@@ -36,6 +36,7 @@
 #include "delayed-int.h"
 #include "gbuffercache.h"
 #include "../core/global.h"
+#include "../core/nproc.h"
 
 
 
@@ -986,7 +987,7 @@ void g_width_tracker_build_initial_cache(GWidthTracker *tracker, wgroup_id_t gid
 
     /* Lancement des traitements */
 
-    runs_count = g_get_num_processors();
+    runs_count = get_max_online_threads();
 
     updates = (GWidthUpdate **)calloc(runs_count, sizeof(GWidthUpdate *));
 
diff --git a/src/glibext/signal.c b/src/glibext/signal.c
index c1a977d..9a3c0e3 100644
--- a/src/glibext/signal.c
+++ b/src/glibext/signal.c
@@ -1,6 +1,6 @@
 
 /* Chrysalide - Outil d'analyse de fichiers binaires
- * signal.h - prototypes pour un encadrement des signaux supplémentaire par rapport à celui de la GLib
+ * signal.c - encadrement des signaux supplémentaire par rapport à celui de la GLib
  *
  * Copyright (C) 2014-2017 Cyrille Bagard
  *
-- 
cgit v0.11.2-87-g4458