Add partial name-to-mib functionality

This commit is contained in:
Yinan Zhang 2020-08-13 11:28:22 -07:00
parent f2e1a5be77
commit 006dd0414e
3 changed files with 89 additions and 1 deletions

View File

@ -98,9 +98,10 @@ typedef struct ctl_arenas_s {
int ctl_byname(tsd_t *tsd, const char *name, void *oldp, size_t *oldlenp, int ctl_byname(tsd_t *tsd, const char *name, void *oldp, size_t *oldlenp,
void *newp, size_t newlen); void *newp, size_t newlen);
int ctl_nametomib(tsd_t *tsd, const char *name, size_t *mibp, size_t *miblenp); int ctl_nametomib(tsd_t *tsd, const char *name, size_t *mibp, size_t *miblenp);
int ctl_bymib(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp, int ctl_bymib(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,
size_t *oldlenp, void *newp, size_t newlen); size_t *oldlenp, void *newp, size_t newlen);
int ctl_mibnametomib(tsd_t *tsd, size_t *mib, size_t miblen, const char *name,
size_t *miblenp);
bool ctl_boot(void); bool ctl_boot(void);
void ctl_prefork(tsdn_t *tsdn); void ctl_prefork(tsdn_t *tsdn);
void ctl_postfork_parent(tsdn_t *tsdn); void ctl_postfork_parent(tsdn_t *tsdn);

View File

@ -1528,6 +1528,36 @@ label_return:
return(ret); return(ret);
} }
int
ctl_mibnametomib(tsd_t *tsd, size_t *mib, size_t miblen, const char *name,
size_t *miblenp) {
int ret;
const ctl_named_node_t *node;
if (!ctl_initialized && ctl_init(tsd)) {
ret = EAGAIN;
goto label_return;
}
ret = ctl_lookupbymib(tsd_tsdn(tsd), &node, mib, miblen);
if (ret != 0) {
goto label_return;
}
if (node == NULL || node->ctl != NULL) {
ret = ENOENT;
goto label_return;
}
assert(miblenp != NULL);
assert(*miblenp >= miblen);
*miblenp -= miblen;
ret = ctl_lookup(tsd_tsdn(tsd), node, name, NULL, mib + miblen,
miblenp);
*miblenp += miblen;
label_return:
return(ret);
}
bool bool
ctl_boot(void) { ctl_boot(void) {
if (malloc_mutex_init(&ctl_mtx, "ctl", WITNESS_RANK_CTL, if (malloc_mutex_init(&ctl_mtx, "ctl", WITNESS_RANK_CTL,

View File

@ -1,5 +1,6 @@
#include "test/jemalloc_test.h" #include "test/jemalloc_test.h"
#include "jemalloc/internal/ctl.h"
#include "jemalloc/internal/hook.h" #include "jemalloc/internal/hook.h"
#include "jemalloc/internal/util.h" #include "jemalloc/internal/util.h"
@ -131,6 +132,61 @@ TEST_BEGIN(test_mallctlnametomib_short_name) {
} }
TEST_END TEST_END
TEST_BEGIN(test_mallctlmibnametomib) {
size_t mib[4];
size_t miblen = 4;
uint32_t result, result_ref;
size_t len_result = sizeof(uint32_t);
tsd_t *tsd = tsd_fetch();
/* Error cases */
assert_d_eq(ctl_mibnametomib(tsd, mib, 0, "bob", &miblen), ENOENT, "");
assert_zu_eq(miblen, 4, "");
assert_d_eq(ctl_mibnametomib(tsd, mib, 0, "9999", &miblen), ENOENT, "");
assert_zu_eq(miblen, 4, "");
/* Valid case. */
assert_d_eq(ctl_mibnametomib(tsd, mib, 0, "arenas", &miblen), 0, "");
assert_zu_eq(miblen, 1, "");
miblen = 4;
assert_d_eq(ctl_mibnametomib(tsd, mib, 1, "bin", &miblen), 0, "");
assert_zu_eq(miblen, 2, "");
expect_d_eq(mallctlbymib(mib, miblen, &result, &len_result, NULL, 0),
ENOENT, "mallctlbymib() should fail on partial path");
/* Error cases. */
miblen = 4;
assert_d_eq(ctl_mibnametomib(tsd, mib, 2, "bob", &miblen), ENOENT, "");
assert_zu_eq(miblen, 4, "");
assert_d_eq(ctl_mibnametomib(tsd, mib, 2, "9999", &miblen), ENOENT, "");
assert_zu_eq(miblen, 4, "");
/* Valid case. */
assert_d_eq(ctl_mibnametomib(tsd, mib, 2, "0", &miblen), 0, "");
assert_zu_eq(miblen, 3, "");
expect_d_eq(mallctlbymib(mib, miblen, &result, &len_result, NULL, 0),
ENOENT, "mallctlbymib() should fail on partial path");
/* Error cases. */
miblen = 4;
assert_d_eq(ctl_mibnametomib(tsd, mib, 3, "bob", &miblen), ENOENT, "");
assert_zu_eq(miblen, 4, "");
assert_d_eq(ctl_mibnametomib(tsd, mib, 3, "9999", &miblen), ENOENT, "");
assert_zu_eq(miblen, 4, "");
/* Valid case. */
assert_d_eq(ctl_mibnametomib(tsd, mib, 3, "nregs", &miblen), 0, "");
assert_zu_eq(miblen, 4, "");
assert_d_eq(mallctlbymib(mib, miblen, &result, &len_result, NULL, 0),
0, "Unexpected mallctlbymib() failure");
assert_d_eq(mallctl("arenas.bin.0.nregs", &result_ref, &len_result,
NULL, 0), 0, "Unexpected mallctl() failure");
expect_zu_eq(result, result_ref,
"mallctlbymib() and mallctl() returned different result");
}
TEST_END
TEST_BEGIN(test_mallctl_config) { TEST_BEGIN(test_mallctl_config) {
#define TEST_MALLCTL_CONFIG(config, t) do { \ #define TEST_MALLCTL_CONFIG(config, t) do { \
t oldval; \ t oldval; \
@ -1121,6 +1177,7 @@ main(void) {
test_mallctl_read_write, test_mallctl_read_write,
test_mallctlnametomib_short_mib, test_mallctlnametomib_short_mib,
test_mallctlnametomib_short_name, test_mallctlnametomib_short_name,
test_mallctlmibnametomib,
test_mallctl_config, test_mallctl_config,
test_mallctl_opt, test_mallctl_opt,
test_manpage_example, test_manpage_example,