]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Remove deprecated sbrk in macOS and Windows.
authorMichael Narigon <mnarigon@gmail.com>
Tue, 2 Feb 2021 12:12:08 +0000 (13:12 +0100)
committerEric Bollengier <eric@baculasystems.com>
Wed, 3 Feb 2021 09:12:21 +0000 (10:12 +0100)
This patch adds a new functions to manage heap size
allocation reporting in macOS and Windows systems.
It will be used as a generic heap applocation reporting
in Bacula. This is a port of the work from
Michael Narigon <mnarigon@gmail.com>.

22 files changed:
bacula/src/baconfig.h
bacula/src/dird/bdirjson.c
bacula/src/dird/dird.c
bacula/src/dird/ua_collect.c
bacula/src/dird/ua_status.c
bacula/src/filed/accurate.c
bacula/src/filed/fdcollect.c
bacula/src/filed/filed.c
bacula/src/filed/status.c
bacula/src/lib/bsys.c
bacula/src/lib/protos.h
bacula/src/lib/workq.c
bacula/src/stored/bcopy.c
bacula/src/stored/bextract.c
bacula/src/stored/bls.c
bacula/src/stored/bscan.c
bacula/src/stored/btape.c
bacula/src/stored/cloud_parts.c
bacula/src/stored/cloud_test.c
bacula/src/stored/sdcollect.c
bacula/src/stored/status.c
bacula/src/stored/stored.c

index 7310f8ad9d49762c280095d6ea8ff6df73acebe4..b7f7c29d32fc807bca1fe7c3563a848f23abb7c8 100644 (file)
@@ -121,8 +121,6 @@ void InitWinAPIWrapper();
 
 #define  OSDependentInit()    InitWinAPIWrapper()
 
-#define sbrk(x)  0
-
 #define clear_thread_id(x) memset(&(x), 0, sizeof(x))
 
 #if defined(BUILDING_DLL)
index f37d06ed1eb32dccaf590b054205ed5d70f4b48c..91ffe8ce8e64b4261d50c81da9fdd024d020a793 100644 (file)
@@ -37,7 +37,6 @@ DIRRES *director;                     /* Director resource */
 int FDConnectTimeout;
 int SDConnectTimeout;
 char *configfile = NULL;
-void *start_heap;
 
 /* Globals Imported */
 extern RES_ITEM job_items[];
index 7dd43d8ca5896bee7b0377b3490d0fb7f90aa0c6..5e37b6030c92336ae4d891756326471481d9234c 100644 (file)
@@ -73,7 +73,6 @@ DIRRES *director;                     /* Director resource */
 int FDConnectTimeout;
 int SDConnectTimeout;
 char *configfile = NULL;
-void *start_heap;
 utime_t last_reload_time = 0;
 bstatcollect *statcollector = NULL;
 dirdstatmetrics_t dirstatmetrics;
@@ -259,7 +258,7 @@ int main (int argc, char *argv[])
    /* DELETE ME when bugs in MA1512, MA1632 MA1639 are fixed */
    MA1512_reload_job_end_cb = reload_job_end_cb;
 
-   start_heap = sbrk(0);
+   mark_heap();
    setlocale(LC_ALL, "");
    bindtextdomain("bacula", LOCALEDIR);
    textdomain("bacula");
index 716060fa729ee4a0e83afebc9f5ff9297dc0feb4..92765e78627528b8dc32a889fb56440a69d0a503 100644 (file)
@@ -30,9 +30,6 @@
 #include "bacula.h"
 #include "dird.h"
 
-/* imported variables */
-extern void *start_heap;
-
 /* imported variables */
 static bool collector_threads_started = false;
 
@@ -75,7 +72,7 @@ bool update_permanent_stats(void *data)
    memused = get_memory_info(NULL, 0);
    statcollector->set_value_int64(dirstatmetrics.bacula_dir_memory_heap, memused);
 #else
-   statcollector->set_value_int64(dirstatmetrics.bacula_dir_memory_heap, (char *)sbrk(0)-(char *)start_heap);
+   statcollector->set_value_int64(dirstatmetrics.bacula_dir_memory_heap, heap_used());
 #endif
    statcollector->set_value_int64(dirstatmetrics.bacula_dir_memory_maxbufs, sm_max_buffers);
    statcollector->set_value_int64(dirstatmetrics.bacula_dir_memory_maxbytes, sm_max_bytes);
index d11e05afb25e70349642ddb118ed4fae01ad2f4a..b13304c4951c265e956219fdebba22b58d35b898 100644 (file)
@@ -28,7 +28,6 @@
 #include "bacula.h"
 #include "dird.h"
 
-extern void *start_heap;
 extern utime_t last_reload_time;
 
 static void list_scheduled_jobs(UAContext *ua);
@@ -489,7 +488,7 @@ void list_dir_status_header(UAContext *ua)
    ua->send_msg(_(" Crypto: fips=%s crypto=%s\n"), crypto_get_fips_enabled(), crypto_get_version());
 
    ua->send_msg(_(" Heap: heap=%s smbytes=%s max_bytes=%s bufs=%s max_bufs=%s\n"),
-      edit_uint64_with_commas((char *)sbrk(0)-(char *)start_heap, b1),
+      edit_uint64_with_commas(heap_used(), b1),
       edit_uint64_with_commas(sm_bytes, b2),
       edit_uint64_with_commas(sm_max_bytes, b3),
       edit_uint64_with_commas(sm_buffers, b4),
index 1951d5a1f4e57cce3ebbbee90b1653dc695425b2..ec61254a9696d8ff3152e82594dea3c183d9229c 100644 (file)
@@ -636,11 +636,9 @@ int accurate_cmd(JCR *jcr)
    }
 
 #ifdef DEBUG
-   extern void *start_heap;
-
    char b1[50], b2[50], b3[50], b4[50], b5[50];
    Dmsg5(dbglvl," Heap: heap=%s smbytes=%s max_bytes=%s bufs=%s max_bufs=%s\n",
-         edit_uint64_with_commas((char *)sbrk(0)-(char *)start_heap, b1),
+         edit_uint64_with_commas(heap_used(), b1),
          edit_uint64_with_commas(sm_bytes, b2),
          edit_uint64_with_commas(sm_max_bytes, b3),
          edit_uint64_with_commas(sm_buffers, b4),
index 75ad4ce19eb6dba4fc9a2db472bdcec639ad71a0..2b5e706cc06fc08b6db6a6b002289960b8595933 100644 (file)
@@ -29,7 +29,6 @@
 #include "filed.h"
 
 /* imported functions and variables */
-extern void *start_heap;
 extern const char collect_all_cmd[];
 extern const char collect_metrics_cmd[];
 
@@ -83,7 +82,7 @@ bool update_permanent_stats(void *data)
    memused = get_memory_info(NULL, 0);
    statcollector->set_value_int64(fdstatmetrics.bacula_client_memory_heap, memused);
 #else
-   statcollector->set_value_int64(fdstatmetrics.bacula_client_memory_heap, (char *)sbrk(0)-(char *)start_heap);
+   statcollector->set_value_int64(fdstatmetrics.bacula_client_memory_heap, heap_used());
 #endif
    statcollector->set_value_int64(fdstatmetrics.bacula_client_memory_maxbufs, sm_max_buffers);
    statcollector->set_value_int64(fdstatmetrics.bacula_client_memory_maxbytes, sm_max_bytes);
index fd171d01b697e0c93018f460f7eb549dc9c5255e..0b045779cd64b78690e0678f176169ac1e6c80a0 100644 (file)
@@ -36,7 +36,6 @@ static bool check_resources();
 /* Exported variables */
 CLIENT *me;                           /* my resource */
 bool no_signals = false;
-void *start_heap;
 extern struct s_cmds cmds[];
 bstatcollect *statcollector = NULL;
 fdstatmetrics_t fdstatmetrics;
@@ -95,7 +94,7 @@ int main (int argc, char *argv[])
    char *uid = NULL;
    char *gid = NULL;
 
-   start_heap = sbrk(0);
+   mark_heap();
    setlocale(LC_ALL, "");
    bindtextdomain("bacula", LOCALEDIR);
    textdomain("bacula");
index 04087ab9e8227d639078fa978e17e6c70caac284..91895f0d4b123032c7b0c141515cf35ac86db2d4 100644 (file)
@@ -27,8 +27,6 @@
 #include "filed.h"
 #include "lib/status.h"
 
-extern void *start_heap;
-
 extern bool GetWindowsVersionString(char *buf, int maxsiz);
 
 
@@ -134,7 +132,7 @@ static void  list_status_header(STATUS_PKT *sp)
 {
    POOL_MEM msg(PM_MESSAGE);
    char b1[32], b2[32], b3[32], b4[32], b5[35];
-   int64_t memused = (char *)sbrk(0)-(char *)start_heap;
+   int64_t memused = heap_used();
    int len;
    char dt[MAX_TIME_LENGTH];
 
index 72539ffdaddc3cd62b6fb9668041db810ddfa06a..95b42d44ced0a737325f6bbc0c5da699d2dd0e86 100644 (file)
@@ -1191,7 +1191,46 @@ int fs_get_free_space(const char *path, int64_t *freeval, int64_t *totalval)
    return -1;
 }
 
-/* This function is used after a fork, the memory manager is not be initialized
+#if defined(HAVE_DARWIN_OS)
+#include <malloc/malloc.h>
+#endif
+
+/*
+ * Determine the amount of heap used
+ * macOS - sbrk(0) is deprecated, use malloc info
+ * Windows - not implemented
+ * others - use sbrk(0)
+ */
+
+/* the initial heap value */
+static int64_t start_heap = 0;
+
+void mark_heap()
+{
+#if defined(HAVE_WIN32)
+   start_heap = 0;
+#elif defined(HAVE_DARWIN_OS)
+   struct mstats ms = mstats();
+   start_heap = (int64_t) ms.bytes_used;
+#else
+   start_heap = (int64_t) sbrk(0);
+#endif
+}
+
+int64_t heap_used()
+{
+#if defined(HAVE_WIN32)
+   return get_memory_info(NULL, 0);
+#elif defined(HAVE_DARWIN_OS)
+   struct mstats ms = mstats();
+   return (int64_t) ms.bytes_used - start_heap;
+#else
+   return (int64_t) sbrk(0) - start_heap;
+#endif
+}
+
+/*
+ * This function is used after a fork, the memory manager is not be initialized
  * properly, so we must stay simple.
  */
 void setup_env(char *envp[])
@@ -1470,7 +1509,7 @@ uint64_t bget_max_mlock(int64_t value)
       if (value < 0) {
          Dmsg0(50, "Limit incorrect set, use the maximum for mlock_max\n");
          /* Request to keep 2GB, we have only 1GB, something is incorrect, so
-          * we take the maximum 
+          * we take the maximum
           */
          value = sys;
       }
@@ -1541,7 +1580,7 @@ static int init_size=1024;
 static int dbglevel=500;
 
 /* alist(100, owned_by_alist) */
-/* return 0: ok, -1: error, 1: not found 
+/* return 0: ok, -1: error, 1: not found
  * Will return a list of users for a group. We look for /etc/groups
  * and in /etc/passwd
  */
@@ -1574,7 +1613,7 @@ again:
 
    } else if (ret == EINTR) {
       goto again;
-      
+
    } else if (ret != 0) {
       berrno be;
       Dmsg1(dbglevel, "Got error for getgrnam_r %s\n", be.bstrerror(ret));
@@ -1656,7 +1695,7 @@ int get_user_home_directory(const char *user, POOLMEM *&home)
    struct passwd pw, *ppw;
    int size = init_size;
    char *buf = (char *)malloc(size);
-   
+
 again:
    errno = 0;
    ret =  getpwnam_r(user, &pw, buf, size, &ppw);
@@ -1901,7 +1940,7 @@ int main(int argc, char **argv)
    POOL_MEM home;
    char *name;
    int ret;
-   
+
    ret = get_group_members("bin", &a);
    ok(ret == 0, "get_group_members()");
    ok(a.size() > 0, "get_group_members() size");
@@ -1919,7 +1958,7 @@ int main(int argc, char **argv)
    }
 
    ok(get_user_home_directory("root", home.addr()) == 0, "get_user_home_directory()");
+
    return report();
 }
 #endif
index 1917b9a4a6baa49c05f339a6d6f1044be021b797..1acae333162d0c2d18ed244c836d11c626914d96 100644 (file)
@@ -125,6 +125,8 @@ void      gdb_print_local(int level);
 
 int       safer_unlink(const char *pathname, const char *regex);
 int fs_get_free_space(const char *path, int64_t *freeval, int64_t *totalval);
+void      mark_heap();
+int64_t   heap_used();
 
 /* bnet.c */
 bool       bnet_tls_server       (TLS_CONTEXT *ctx, BSOCK *bsock,
index 232c8a29108f645c6eb153557a50f3f8fa78ca41..09a6c7cdf486f3eb56ba32222e154e34d0a51dd2 100644 (file)
@@ -448,9 +448,7 @@ int main (int argc, char *argv[])
 {
    pthread_attr_t attr;
 
-   void * start_heap = sbrk(0);
-   (void)start_heap;
-
+   mark_heap();
    setlocale(LC_ALL, "");
    bindtextdomain("bacula", LOCALEDIR);
    textdomain("bacula");
index a86087022368ea7b23770fc2c9c871a1654ebadc..573db3dbb25ce33f2f2d5d65d53bbc296abb03e9 100644 (file)
@@ -50,7 +50,6 @@ static SESSION_LABEL sessrec;
 static CONFIG *config;
 #define CONFIG_FILE "bacula-sd.conf"
 
-void *start_heap;
 char *configfile = NULL;
 
 static void usage()
index ee67c2c631295b90baa2e3f12a05fa3fc49a2616..2621e84b3c84a08c1bea045c70bba50929cd9258 100644 (file)
@@ -67,7 +67,6 @@ static uint64_t fileAddr = 0;         /* file write address */
 static CONFIG *config;
 #define CONFIG_FILE "bacula-sd.conf"
 
-void *start_heap;
 char *configfile = NULL;
 bool skip_extract = false;
 
index 8a97008fae4f4c8e440967c7db8297b79a7d3202..d320dfe1610215e19adc6e60ecb74094ee46fa08 100644 (file)
@@ -54,7 +54,6 @@ static CONFIG *config;
 static bool filter = false;                  /* any filter set ? */
 static bool dedup = false;                   /* decode dedup reference */
 
-void *start_heap;
 #define CONFIG_FILE "bacula-sd.conf"
 char *configfile = NULL;
 bool detect_errors = false;
index bd7b5075a5c9e77962c9d16307c761ebe98f7c6c..b092915a12f79ba0f68b14d00d31625c62da855b 100644 (file)
@@ -96,7 +96,6 @@ static int num_files = 0;
 static CONFIG *config;
 #define CONFIG_FILE "bacula-sd.conf"
 
-void *start_heap;
 char *configfile = NULL;
 
 static void usage()
index 5c8def1ef0fdf67ad32399251966b54a7f028fd2..359c5d5e96612d579e44d4daf531fff510b77f4d 100644 (file)
@@ -42,7 +42,6 @@ extern bool parse_sd_config(CONFIG *config, const char *configfile, int exit_cod
 extern void free_config_resources();
 
 /* Exported variables */
-void *start_heap;
 int quit = 0;
 char buf[100000];
 int bsize = TAPE_BSIZE;
index c279ac0ad3ed9cc75bd953fbffe96c3383a72b71..cab9a4d58d4617e27f5b56fa7b5212407dce1ccd 100644 (file)
@@ -335,9 +335,7 @@ int main (int argc, char *argv[])
 {
    pthread_attr_t attr;
 
-   void * start_heap = sbrk(0);
-   (void)start_heap;
-
+   mark_heap();
    setlocale(LC_ALL, "");
    bindtextdomain("bacula", LOCALEDIR);
    textdomain("bacula");
index 7972611e40e9da8f24e65459ddab04e2567f626a..7662ac51efd49483cc59844c92a229ba38c3daf9 100644 (file)
@@ -24,7 +24,6 @@ extern bool parse_sd_config(CONFIG *config, const char *configfile, int exit_cod
 
 static CONFIG *config;
 
-void *start_heap;
 #define CONFIG_FILE "bacula-sd.conf"
 char *configfile = NULL;
 bool detect_errors = false;
index 482b507be562aa30676cd9bd2dc1500ea9b43353..87a79de884bde3bc5b56817abb35055f8bc2e29e 100644 (file)
@@ -27,7 +27,6 @@
 #include "stored.h"
 
 /* imported functions and variables */
-extern void *start_heap;
 extern const char collect_all_cmd[];
 extern const char collect_metrics_cmd[];
 extern bool init_done;
@@ -102,7 +101,7 @@ bool update_permanent_stats(void *data)
    memused = get_memory_info(NULL, 0);
    statcollector->set_value_int64(sdstatmetrics.bacula_storage_memory_heap, memused);
 #else
-   statcollector->set_value_int64(sdstatmetrics.bacula_storage_memory_heap, (char *)sbrk(0)-(char *)start_heap);
+   statcollector->set_value_int64(sdstatmetrics.bacula_storage_memory_heap, heap_used());
 #endif
    statcollector->set_value_int64(sdstatmetrics.bacula_storage_memory_maxbufs, sm_max_buffers);
    statcollector->set_value_int64(sdstatmetrics.bacula_storage_memory_maxbytes, sm_max_bytes);
index 86e583e42f3057881fa8735cf4616ab101f3edd5..16a1e063f5c93d8482d45871d60faabc3eb47f76 100644 (file)
@@ -34,7 +34,6 @@ extern void dbg_print_plugin(FILE *fp);
 
 /* Imported variables */
 extern BSOCK *filed_chan;
-extern void *start_heap;
 
 /* Static variables */
 static char OKqstatus[]   = "3000 OK .status\n";
@@ -598,7 +597,7 @@ static void list_status_header(STATUS_PKT *sp)
    list_resource_limits(sp, nofile_l, memlock_l);
 
    len = Mmsg(msg, _(" Heap: heap=%s smbytes=%s max_bytes=%s bufs=%s max_bufs=%s\n"),
-         edit_uint64_with_commas((char *)sbrk(0)-(char *)start_heap, b1),
+         edit_uint64_with_commas(heap_used(), b1),
          edit_uint64_with_commas(sm_bytes, b2),
          edit_uint64_with_commas(sm_max_bytes, b3),
          edit_uint64_with_commas(sm_buffers, b4),
index fc863e81c05631125ff21e4e58b7b2791b791893..03ffe8be7556f270e3a23605120e30de6b3a4709 100644 (file)
@@ -52,7 +52,6 @@ extern "C" void *device_initialization(void *arg);
 /* Global variables exported */
 char OK_msg[]   = "3000 OK\n";
 char TERM_msg[] = "3999 Terminate\n";
-void *start_heap;
 static bool test_config = false;
 bstatcollect *statcollector = NULL;
 sdstatmetrics_t sdstatmetrics;
@@ -132,7 +131,7 @@ int main (int argc, char *argv[])
 
    device_default_open_mode = omd_write;
 
-   start_heap = sbrk(0);
+   mark_heap();
    setlocale(LC_ALL, "");
    bindtextdomain("bacula", LOCALEDIR);
    textdomain("bacula");