gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libmicrohttpd] branch master updated (602ad551 -> 4453fd20)


From: gnunet
Subject: [libmicrohttpd] branch master updated (602ad551 -> 4453fd20)
Date: Wed, 06 Sep 2023 16:42:17 +0200

This is an automated email from the git hooks/post-receive script.

karlson2k pushed a change to branch master
in repository libmicrohttpd.

    from 602ad551 Revert "fixing #7772 exactly as suggested by reporter"
     new 2ba3d8da W32 projects: added .editorconfig
     new cd16ca4d Implemented detection of number of CPUs available for the 
process on W32
     new bf99fa82 perf_replies: minor improvement for W32 code
     new 4453fd20 mhd_tool_get_cpu_count: fixed code style

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/tools/mhd_tool_get_cpu_count.c | 262 +++++++++++++++++++++++++++++++++----
 w32/.editorconfig                  |  87 ++++++++++++
 2 files changed, 321 insertions(+), 28 deletions(-)
 create mode 100644 w32/.editorconfig

diff --git a/src/tools/mhd_tool_get_cpu_count.c 
b/src/tools/mhd_tool_get_cpu_count.c
index 9addf4be..dc01e51e 100644
--- a/src/tools/mhd_tool_get_cpu_count.c
+++ b/src/tools/mhd_tool_get_cpu_count.c
@@ -70,6 +70,9 @@
 #ifdef HAVE_SYS_CPUSET_H
 #  include <sys/cpuset.h>
 #endif /* HAVE_SYS_CPUSET_H */
+#ifdef HAVE_STDBOOL_H
+#  include <stdbool.h>
+#endif /* HAVE_STDBOOL_H */
 
 #if ! defined(HAS_DECL_CPU_SETSIZE) && ! defined(CPU_SETSIZE)
 #  define CPU_SETSIZE (1024)
@@ -131,7 +134,7 @@ mhd_tool_get_proc_cpu_count_sched_getaffinity_ (void)
   if (0 >= ret)
   {
     cpu_set_t cur_set;
-    if (0 == sched_getaffinity (getpid (), sizeof(cur_set), &cur_set))
+    if (0 == sched_getaffinity (getpid (), sizeof (cur_set), &cur_set))
     {
 #ifdef HAVE_CPU_COUNT
       ret = CPU_COUNT (&cur_set);
@@ -197,7 +200,7 @@ mhd_tool_get_proc_cpu_count_cpuset_getaffinity_ (void)
     /* The should get "anonymous" mask/set. The anonymous mask is always
        a subset of the assigned set (which is a subset of the root set). */
     if (0 == cpuset_getaffinity (CPU_LEVEL_WHICH, CPU_WHICH_PID, (id_t) -1,
-                                 sizeof(cur_mask), &cur_mask))
+                                 sizeof (cur_mask), &cur_mask))
     {
 #ifdef HAVE_CPU_COUNT
       ret = CPU_COUNT (&cur_mask);
@@ -311,6 +314,203 @@ mhd_tool_get_proc_cpu_count_sched_getaffinity_np_ (void)
 }
 
 
+/**
+ * Detect the number of logical CPU cores available for the process by
+ * W32 API functions.
+ * @return the number of detected logical CPU cores or
+ *         -1 if failed to detect (or this function unavailable).
+ */
+static int
+mhd_tool_get_proc_cpu_count_w32_ (void)
+{
+  int ret = -1;
+#if defined(_WIN32) && ! defined(__CYGWIN__)
+  /* W32 Native */
+  /**
+   * Maximum used number of CPU groups.
+   * Improvement: Implement dynamic allocation when it would be reasonable
+   */
+#define MHDT_MAX_GROUP_COUNT 128
+  /**
+   * The count of logical CPUs as returned by GetProcessAffinityMask()
+   */
+  int count_by_proc_aff_mask;
+  count_by_proc_aff_mask = -1;
+  if (1)
+  {
+    DWORD_PTR proc_aff;
+    DWORD_PTR sys_aff;
+
+    if (GetProcessAffinityMask (GetCurrentProcess (), &proc_aff, &sys_aff))
+    {
+      /* Count all set bits */
+      for (count_by_proc_aff_mask = 0; 0 != proc_aff; proc_aff &= proc_aff - 1)
+        ++count_by_proc_aff_mask;
+    }
+  }
+  if (0 < count_by_proc_aff_mask)
+  {
+    HMODULE k32hndl;
+    k32hndl = LoadLibraryA ("kernel32.dll");
+    if (NULL != k32hndl)
+    {
+      typedef BOOL (WINAPI *GPGA_PTR)(HANDLE hProcess,
+                                      PUSHORT GroupCount,
+                                      PUSHORT GroupArray);
+      GPGA_PTR ptrGetProcessGroupAffinity;
+      ptrGetProcessGroupAffinity =
+        (GPGA_PTR) (void *) GetProcAddress (k32hndl,
+                                            "GetProcessGroupAffinity");
+      if (NULL == ptrGetProcessGroupAffinity)
+      {
+        /* Windows version before Win7 */
+        /* No processor groups supported, the process affinity mask gives full 
picture */
+        ret = count_by_proc_aff_mask;
+      }
+      else
+      {
+        /* Windows version Win7 or later */
+        /* Processor groups are supported */
+        USHORT arr_elements = MHDT_MAX_GROUP_COUNT;
+        USHORT groups_arr[MHDT_MAX_GROUP_COUNT]; /* Hopefully should be enough 
*/
+        /* Improvement: Implement dynamic allocation when it would be 
reasonable */
+        /**
+         * Exactly one processor group is assigned to the process
+         */
+        bool single_cpu_group_assigned; /**< Exactly one processor group is 
assigned to the process */
+        struct mhdt_GR_AFFINITY
+        {
+          KAFFINITY Mask;
+          WORD Group;
+          WORD Reserved[3];
+        };
+        typedef BOOL (WINAPI *GPDCSM_PTR)(HANDLE Process,
+                                          struct mhdt_GR_AFFINITY *CpuSetMasks,
+                                          USHORT CpuSetMaskCount,
+                                          USHORT *RequiredMaskCount);
+        GPDCSM_PTR ptrGetProcessDefaultCpuSetMasks;
+        bool win_fe_or_later;
+        bool cpu_set_mask_assigned;
+
+        single_cpu_group_assigned = false;
+        if (ptrGetProcessGroupAffinity (GetCurrentProcess (), &arr_elements,
+                                        groups_arr))
+        {
+          if (1 == arr_elements)
+          {
+            /* Exactly one processor group assigned to the process */
+            single_cpu_group_assigned = true;
+#if 0 /* Disabled code */
+            /* The value returned by GetThreadGroupAffinity() is not relevant 
as
+               for the new threads the process affinity mask is used. */
+            ULONG_PTR proc_aff2;
+            typedef BOOL (WINAPI *GTGA_PTR)(HANDLE hThread,
+                                            struct mhdt_GR_AFFINITY *
+                                            GroupAffinity);
+            GTGA_PTR ptrGetThreadGroupAffinity;
+            ptrGetThreadGroupAffinity =
+              (GTGA_PTR) (void *) GetProcAddress (k32hndl,
+                                                  "GetThreadGroupAffinity");
+            if (NULL != ptrGetThreadGroupAffinity)
+            {
+              struct mhdt_GR_AFFINITY thr_gr_aff;
+              if (ptrGetThreadGroupAffinity (GetCurrentThread (), &thr_gr_aff))
+                proc_aff2 = (ULONG_PTR) thr_gr_aff.Mask;
+            }
+#endif /* Disabled code */
+          }
+        }
+        ptrGetProcessDefaultCpuSetMasks =
+          (GPDCSM_PTR) (void *) GetProcAddress (k32hndl,
+                                                
"GetProcessDefaultCpuSetMasks");
+        if (NULL != ptrGetProcessDefaultCpuSetMasks)
+        {
+          /* This is Iron Release / Codename Fe
+             (also know as Windows 11 and Windows Server 2022)
+             or later version */
+          struct mhdt_GR_AFFINITY gr_affs[MHDT_MAX_GROUP_COUNT]; /* Hopefully 
should be enough */
+          /* Improvement: Implement dynamic allocation when it would be 
reasonable */
+          USHORT num_elm;
+
+          win_fe_or_later = true;
+
+          if (ptrGetProcessDefaultCpuSetMasks (GetCurrentProcess (), gr_affs,
+                                               sizeof (gr_affs)
+                                               / sizeof (gr_affs[0]), 
&num_elm))
+          {
+            if (0 == num_elm)
+            {
+              /* No group mask set */
+              cpu_set_mask_assigned = false;
+            }
+            else
+              cpu_set_mask_assigned = true;
+          }
+          else
+            cpu_set_mask_assigned = true; /* Assume the worst case */
+        }
+        else
+        {
+          win_fe_or_later = false;
+          cpu_set_mask_assigned = false;
+        }
+        if (! win_fe_or_later)
+        {
+          /* The OS is not capable of distributing threads across different
+             processor groups. Results reported by GetProcessAffinityMask()
+             are relevant for the main processor group for the process. */
+          ret = count_by_proc_aff_mask;
+        }
+        else
+        {
+          /* The of is capable of automatic threads distribution across
+             processor groups. */
+          if (cpu_set_mask_assigned)
+          {
+            /* Assigned Default CpuSet Masks combines with "classic"
+               affinity in the not fully clear way. The combination
+               is not documented and this functionality could be changed
+               any moment. */
+            ret = -1;
+          }
+          else
+          {
+            if (! single_cpu_group_assigned)
+            {
+              /* This is a multi processor group process on Win11 (or later).
+                 Each processor group may have different affinity and
+                 the OS has not API to get it.
+                 For example, affinity to the main processor group could be
+                 assigned by SetProcessAffinityMask() function, which converts
+                 the process to the single-processor-group type, but if
+                 SetThreadGroupAffinity() is called later and bind the thread
+                 to another processor group, the process becomes 
multi-processor-
+                 group again, however the initial affinity mask is still used
+                 for the initial (main) processor group. There is no API to 
read
+                 it.
+                 It is also possible that processor groups have different 
number
+                 of processors. */
+              ret = -1;
+            }
+            else
+            {
+              /* Single-processor-group process on Win11 (or later) without
+                 assigned Default CpuSet Masks. */
+              ret = count_by_proc_aff_mask;
+            }
+          }
+        }
+      }
+      FreeLibrary (k32hndl);
+    }
+  }
+#endif /* _WIN32 && ! __CYGWIN__ */
+  if (0 >= ret)
+    return -1;
+  return ret;
+}
+
+
 /**
  * Detect the number of logical CPU cores available for the process.
  * The number of cores available for this process could be different from
@@ -352,6 +552,10 @@ mhd_tool_get_proc_cpu_count (void)
   if (0 < res)
     return res;
 
+  res = mhd_tool_get_proc_cpu_count_w32_ ();
+  if (0 < res)
+    return res;
+
   return -1;
 }
 
@@ -370,8 +574,8 @@ mhd_tool_get_sys_cpu_count_special_api_ (void)
   {
     /* HP-UX things */
     struct pst_dynamic psd_data;
-    memset ((void *) &psd_data, 0, sizeof(psd_data));
-    if (1 == pstat_getdynamic (&psd_data, sizeof(psd_data), (size_t) 1, 0))
+    memset ((void *) &psd_data, 0, sizeof (psd_data));
+    if (1 == pstat_getdynamic (&psd_data, sizeof (psd_data), (size_t) 1, 0))
     {
       if (0 < psd_data.psd_proc_cnt)
         ret = (int) psd_data.psd_proc_cnt;
@@ -394,7 +598,7 @@ mhd_tool_get_sys_cpu_count_special_api_ (void)
   {
     /* Native W32 */
     HMODULE k32hndl;
-    k32hndl = GetModuleHandleA ("kernel32.dll");
+    k32hndl = LoadLibraryA ("kernel32.dll");
     if (NULL != k32hndl)
     {
       typedef DWORD (WINAPI *GAPC_PTR)(WORD GroupNumber);
@@ -423,20 +627,22 @@ mhd_tool_get_sys_cpu_count_special_api_ (void)
       {
         SYSTEM_INFO sysInfo;
 
-        memset ((void *) &sysInfo, 0, sizeof(sysInfo));
+        memset ((void *) &sysInfo, 0, sizeof (sysInfo));
         ptrGetNativeSystemInfo (&sysInfo);
         ret = (int) sysInfo.dwNumberOfProcessors;
         if (sysInfo.dwNumberOfProcessors != (DWORD) ret)
           ret = -1; /* Overflow */
       }
     }
+    if (NULL != k32hndl)
+      FreeLibrary (k32hndl);
   }
   if (0 >= ret)
   {
     /* May give incorrect (low) result on versions from W7 to W11
        when more then 64 CPUs are available */
     SYSTEM_INFO sysInfo;
-    memset ((void *) &sysInfo, 0, sizeof(sysInfo));
+    memset ((void *) &sysInfo, 0, sizeof (sysInfo));
     GetSystemInfo (&sysInfo);
     ret = (int) sysInfo.dwNumberOfProcessors;
     if (sysInfo.dwNumberOfProcessors != (DWORD) ret)
@@ -468,47 +674,47 @@ mhd_tool_get_sys_cpu_count_sysctl_ (void)
 #ifdef HAVE_SYSCTLBYNAME
   if (0 >= ret)
   {
-    size_t value_size = sizeof(ret);
+    size_t value_size = sizeof (ret);
     /* Darwin: The number of available logical CPUs */
     if ((0 != sysctlbyname ("hw.logicalcpu", &ret, &value_size,
                             NULL, 0))
-        || (sizeof(ret) != value_size))
+        || (sizeof (ret) != value_size))
       ret = -1;
   }
   if (0 >= ret)
   {
-    size_t value_size = sizeof(ret);
+    size_t value_size = sizeof (ret);
     /* FreeBSD: The number of online CPUs */
     if ((0 != sysctlbyname ("kern.smp.cpus", &ret, &value_size,
                             NULL, 0))
-        || (sizeof(ret) != value_size))
+        || (sizeof (ret) != value_size))
       ret = -1;
   }
   if (0 >= ret)
   {
-    size_t value_size = sizeof(ret);
+    size_t value_size = sizeof (ret);
     /* Darwin: The current number of CPUs available to run threads */
     if ((0 != sysctlbyname ("hw.activecpu", &ret, &value_size,
                             NULL, 0))
-        || (sizeof(ret) != value_size))
+        || (sizeof (ret) != value_size))
       ret = -1;
   }
   if (0 >= ret)
   {
-    size_t value_size = sizeof(ret);
+    size_t value_size = sizeof (ret);
     /* OpenBSD, NetBSD: The number of online CPUs */
     if ((0 != sysctlbyname ("hw.ncpuonline", &ret, &value_size,
                             NULL, 0))
-        || (sizeof(ret) != value_size))
+        || (sizeof (ret) != value_size))
       ret = -1;
   }
   if (0 >= ret)
   {
-    size_t value_size = sizeof(ret);
+    size_t value_size = sizeof (ret);
     /* Darwin: The old/alternative name for "hw.activecpu" */
     if ((0 != sysctlbyname ("hw.availcpu", &ret, &value_size,
                             NULL, 0))
-        || (sizeof(ret) != value_size))
+        || (sizeof (ret) != value_size))
       ret = -1;
   }
 #endif /* HAVE_SYSCTLBYNAME */
@@ -518,10 +724,10 @@ mhd_tool_get_sys_cpu_count_sysctl_ (void)
   if (0 >= ret)
   {
     /* OpenBSD, NetBSD: The number of online CPUs */
-    int mib[2] = { CTL_HW, HW_NCPUONLINE };
-    size_t value_size = sizeof(ret);
+    int mib[2] = {CTL_HW, HW_NCPUONLINE};
+    size_t value_size = sizeof (ret);
     if ((0 != sysctl (mib, 2, &ret, &value_size, NULL, 0))
-        || (sizeof(ret) != value_size))
+        || (sizeof (ret) != value_size))
       ret = -1;
   }
 #endif /* HAVE_SYSCTL && HAS_DECL_CTL_HW && HAS_DECL_HW_NCPUONLINE */
@@ -531,10 +737,10 @@ mhd_tool_get_sys_cpu_count_sysctl_ (void)
   if (0 >= ret)
   {
     /* Darwin: The MIB name for "hw.activecpu" */
-    int mib[2] = { CTL_HW, HW_AVAILCPU };
-    size_t value_size = sizeof(ret);
+    int mib[2] = {CTL_HW, HW_AVAILCPU};
+    size_t value_size = sizeof (ret);
     if ((0 != sysctl (mib, 2, &ret, &value_size, NULL, 0))
-        || (sizeof(ret) != value_size))
+        || (sizeof (ret) != value_size))
       ret = -1;
   }
 #endif /* HAVE_SYSCTL && HAS_DECL_CTL_HW && HAS_DECL_HW_AVAILCPU */
@@ -566,11 +772,11 @@ mhd_tool_get_sys_cpu_count_sysctl_fallback_ (void)
 #ifdef HAVE_SYSCTLBYNAME
   if (0 >= ret)
   {
-    size_t value_size = sizeof(ret);
+    size_t value_size = sizeof (ret);
     /* FreeBSD, OpenBSD, NetBSD, Darwin (and others?): The number of CPUs */
     if ((0 != sysctlbyname ("hw.ncpu", &ret, &value_size,
                             NULL, 0))
-        || (sizeof(ret) != value_size))
+        || (sizeof (ret) != value_size))
       ret = -1;
   }
 #endif /* HAVE_SYSCTLBYNAME */
@@ -580,10 +786,10 @@ mhd_tool_get_sys_cpu_count_sysctl_fallback_ (void)
   if (0 >= ret)
   {
     /* FreeBSD, OpenBSD, NetBSD, Darwin (and others?): The number of CPUs */
-    int mib[2] = { CTL_HW, HW_NCPU };
-    size_t value_size = sizeof(ret);
+    int mib[2] = {CTL_HW, HW_NCPU};
+    size_t value_size = sizeof (ret);
     if ((0 != sysctl (mib, 2, &ret, &value_size, NULL, 0))
-        || (sizeof(ret) != value_size))
+        || (sizeof (ret) != value_size))
       ret = -1;
   }
 #endif /* HAVE_SYSCTL && HAS_DECL_CTL_HW && HAS_DECL_HW_NCPU */
diff --git a/w32/.editorconfig b/w32/.editorconfig
new file mode 100644
index 00000000..589b91d9
--- /dev/null
+++ b/w32/.editorconfig
@@ -0,0 +1,87 @@
+# Do not check parent dirs for .editorconfig
+root = true
+
+[Makefile{,.in,.am}]
+indent_style = tab
+charset = latin1
+insert_final_newline = true
+
+[*.{c,h}]
+indent_style = space
+charset = utf-8
+insert_final_newline = true
+indent_size = 2
+tab_width = 4
+trim_trailing_whitespace = true
+max_line_length = 80
+
+# Visual C++ Code Style settings
+
+cpp_generate_documentation_comments = doxygen_slash_star
+
+# Visual C++ Formatting settings
+
+cpp_indent_braces = false
+cpp_indent_multi_line_relative_to = innermost_parenthesis
+cpp_indent_within_parentheses = align_to_parenthesis
+cpp_indent_preserve_within_parentheses = true
+cpp_indent_case_contents = true
+cpp_indent_case_labels = false
+cpp_indent_case_contents_when_block = true
+cpp_indent_lambda_braces_when_parameter = true
+cpp_indent_goto_labels = leftmost_column
+cpp_indent_preprocessor = leftmost_column
+cpp_indent_access_specifiers = false
+cpp_indent_namespace_contents = false
+cpp_indent_preserve_comments = true
+cpp_new_line_before_open_brace_namespace = new_line
+cpp_new_line_before_open_brace_type = new_line
+cpp_new_line_before_open_brace_function = new_line
+cpp_new_line_before_open_brace_block = new_line
+cpp_new_line_before_open_brace_lambda = ignore
+cpp_new_line_scope_braces_on_separate_lines = true
+cpp_new_line_close_brace_same_line_empty_type = false
+cpp_new_line_close_brace_same_line_empty_function = false
+cpp_new_line_before_catch = false
+cpp_new_line_before_else = true
+cpp_new_line_before_while_in_do_while = false
+cpp_space_before_function_open_parenthesis = insert
+cpp_space_within_parameter_list_parentheses = false
+cpp_space_between_empty_parameter_list_parentheses = false
+cpp_space_after_keywords_in_control_flow_statements = true
+cpp_space_within_control_flow_statement_parentheses = false
+cpp_space_before_lambda_open_parenthesis = false
+cpp_space_within_cast_parentheses = false
+cpp_space_after_cast_close_parenthesis = true
+cpp_space_within_expression_parentheses = false
+cpp_space_before_block_open_brace = false
+cpp_space_between_empty_braces = false
+cpp_space_before_initializer_list_open_brace = true
+cpp_space_within_initializer_list_braces = false
+cpp_space_preserve_in_initializer_list = true
+cpp_space_before_open_square_bracket = false
+cpp_space_within_square_brackets = false
+cpp_space_before_empty_square_brackets = false
+cpp_space_between_empty_square_brackets = false
+cpp_space_group_square_brackets = true
+cpp_space_within_lambda_brackets = false
+cpp_space_between_empty_lambda_brackets = false
+cpp_space_before_comma = false
+cpp_space_after_comma = true
+cpp_space_remove_around_member_operators = true
+cpp_space_before_inheritance_colon = true
+cpp_space_before_constructor_colon = true
+cpp_space_remove_before_semicolon = true
+cpp_space_after_semicolon = true
+cpp_space_remove_around_unary_operator = false
+cpp_space_around_binary_operator = insert
+cpp_space_around_assignment_operator = insert
+cpp_space_pointer_reference_alignment = right
+cpp_space_around_ternary_operator = insert
+cpp_use_unreal_engine_macro_formatting = false
+cpp_wrap_preserve_blocks = never
+
+# Visual C++ Inlcude Cleanup settings
+
+cpp_include_cleanup_add_missing_error_tag_type = suggestion
+cpp_include_cleanup_remove_unused_error_tag_type = dimmed

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]