提交 aa1f46f7 编写于 作者: M Maxim Polyakov

Fix style issues

Signed-off-by: NMaxim Polyakov <polyakov.maksim@huawei.com>
Change-Id: I395172f55ac387a98f998334c2218d5f86bb35b9
Signed-off-by: NMaxim Polyakov <polyakov.maksim@huawei.com>
上级 20836ff3
......@@ -26,13 +26,14 @@ typedef struct
static const size_t allocs_sizes[ALLOCATIONS_NUMBER] = {
8,
2048,
65536,
524288,
2 * 1024,
64 * 1024,
512 * 1024,
2 * 1024 * 1024,
8 * 1024 * 1024,
16 * 1024 * 1024,
32 * 1024 * 1024};
32 * 1024 * 1024
};
void iterate_callback(void *base, size_t size, void *data)
{
......@@ -112,13 +113,8 @@ int iterate_wrapper(iterate_arg_t *iterate_arg)
{
if (iterate_arg->allocs_reported_number[i] != 1)
{
// printf("Error: failed on %p %lu %lu\n", (void*)iterate_arg->allocs[i], iterate_arg->allocs_actual_sizes[i], iterate_arg->allocs_reported_number[i]);
ret = -1;
}
else
{
// printf("Ok on %p %lu %lu\n", (void*)iterate_arg->allocs[i], iterate_arg->allocs_actual_sizes[i], iterate_arg->allocs_reported_number[i]);
}
}
return ret;
}
......
......@@ -13,42 +13,44 @@ pthread_barrier_t routine_allocated;
const size_t SLEEP_TIME_SECONDS = 1;
void *disable_routine(void *vargp) {
malloc_disable();
pthread_barrier_wait(&routine_disabled);
sleep(SLEEP_TIME_SECONDS);
malloc_enable();
pthread_barrier_wait(&routine_allocated);
return NULL;
void *disable_routine(void *vargp)
{
malloc_disable();
pthread_barrier_wait(&routine_disabled);
sleep(SLEEP_TIME_SECONDS);
malloc_enable();
pthread_barrier_wait(&routine_allocated);
return NULL;
}
int test_malloc_while_disabled(void) {
int ret = 0;
pthread_barrier_init(&routine_disabled, NULL, 2);
pthread_barrier_init(&routine_allocated, NULL, 2);
pthread_t thread_id;
pthread_create(&thread_id, NULL, disable_routine, NULL);
pthread_barrier_wait(&routine_disabled);
time_t start = time(0);
int *x = malloc(sizeof(int));
pthread_barrier_wait(&routine_allocated);
time_t end = time(0);
size_t seconds = end - start;
if (seconds < SLEEP_TIME_SECONDS) {
// printf("elapsed time: %zu, required at least %zu", seconds, SLEEP_TIME_SECONDS);
ret = -1;
}
free(x);
pthread_join(thread_id, NULL);
return ret;
int test_malloc_while_disabled(void)
{
int ret = 0;
pthread_barrier_init(&routine_disabled, NULL, 2);
pthread_barrier_init(&routine_allocated, NULL, 2);
pthread_t thread_id;
pthread_create(&thread_id, NULL, disable_routine, NULL);
pthread_barrier_wait(&routine_disabled);
time_t start = time(0);
int *x = malloc(sizeof(int));
pthread_barrier_wait(&routine_allocated);
time_t end = time(0);
size_t seconds = end - start;
if (seconds < SLEEP_TIME_SECONDS)
{
ret = -1;
}
free(x);
pthread_join(thread_id, NULL);
return ret;
}
int main()
{
int ret = 0;
int main() {
int ret = 0;
ret = check_and_report("Testing malloc while disabled", test_malloc_while_disabled);
ret = check_and_report("Testing malloc while disabled", test_malloc_while_disabled);
return ret;
return ret;
}
......@@ -14,12 +14,12 @@
#include "test.h"
#define MAX_TID_LEN 32
#define BUFFER_SIZE 4096
#define STATS_BUFFER_SIZE 4096
typedef struct
{
char stats_after_allocations[BUFFER_SIZE];
char stats_after_free[BUFFER_SIZE];
char stats_after_allocations[STATS_BUFFER_SIZE];
char stats_after_free[STATS_BUFFER_SIZE];
char threads[SIZES_COUNT][MAX_TID_LEN + 1];
} test_results_t;
......@@ -48,7 +48,7 @@ int stats_to_buffer(char *buffer)
close(err_pipe[1]);
stderr_stats_cb();
fflush(stderr);
read(err_pipe[0], buffer, BUFFER_SIZE);
read(err_pipe[0], buffer, STATS_BUFFER_SIZE);
dup2(saved_stderr, STDERR_FILENO);
return 1;
}
......
......@@ -5,19 +5,22 @@
static const xmlChar *get_text_from_children(xmlNodePtr children)
{
for (xmlNodePtr child_node = children; child_node != NULL; child_node = child_node->next) {
if (child_node->type == XML_TEXT_NODE) {
for (xmlNodePtr child_node = children; child_node != NULL; child_node = child_node->next)
{
if (child_node->type == XML_TEXT_NODE)
{
return child_node->content;
}
}
return NULL;
}
static const xmlChar *get_attribute(const char *attr_name, xmlNodePtr node)
{
for (xmlAttrPtr curr_attr = node->properties; curr_attr != NULL; curr_attr = curr_attr->next) {
if (xmlStrEqual(curr_attr->name, (const xmlChar *) attr_name)) {
for (xmlAttrPtr curr_attr = node->properties; curr_attr != NULL; curr_attr = curr_attr->next)
{
if (xmlStrEqual(curr_attr->name, (const xmlChar *)attr_name))
{
return get_text_from_children(curr_attr->children);
}
}
......@@ -27,15 +30,20 @@ static const xmlChar *get_attribute(const char *attr_name, xmlNodePtr node)
static xmlNodePtr
find_child_node_with_attr(const char *name, const char *attr_name, const char *attr_value, xmlNodePtr parent)
{
if (parent == NULL) {
if (parent == NULL)
{
return NULL;
}
for (xmlNodePtr curr_node = parent->children; curr_node != NULL; curr_node = curr_node->next) {
if (curr_node->type == XML_ELEMENT_NODE && xmlStrEqual(curr_node->name, (xmlChar *) name)) {
if (attr_name == NULL) {
for (xmlNodePtr curr_node = parent->children; curr_node != NULL; curr_node = curr_node->next)
{
if (curr_node->type == XML_ELEMENT_NODE && xmlStrEqual(curr_node->name, (xmlChar *)name))
{
if (attr_name == NULL)
{
return curr_node;
}
if (xmlStrEqual(get_attribute(attr_name, curr_node), (const xmlChar *) attr_value)) {
if (xmlStrEqual(get_attribute(attr_name, curr_node), (const xmlChar *)attr_value))
{
return curr_node;
}
}
......@@ -50,10 +58,11 @@ static xmlNodePtr find_child_node(const char *name, xmlNodePtr parent)
static const char *get_node_text(xmlNodePtr node_ptr)
{
if (node_ptr == NULL) {
if (node_ptr == NULL)
{
return NULL;
}
return (const char *) get_text_from_children(node_ptr->children);
return (const char *)get_text_from_children(node_ptr->children);
}
static void stderr_stats_cb(void)
......@@ -63,15 +72,18 @@ static void stderr_stats_cb(void)
static long long parse_amount(const char *s)
{
if (s == NULL) {
if (s == NULL)
{
return -1;
}
char *end_ptr;
long long result = strtoll(s, &end_ptr, 10);
if (end_ptr != s + strlen(s)) {
if (end_ptr != s + strlen(s))
{
return -1;
}
if (result < 0) {
if (result < 0)
{
return -1;
}
return result;
......@@ -79,27 +91,32 @@ static long long parse_amount(const char *s)
static int populate_thread_stats(const char *input, const char *thread_id, malloc_thread_stats_t *stats)
{
xmlDocPtr doc_ptr = xmlParseDoc((const xmlChar *) input);
if (doc_ptr == NULL) {
xmlDocPtr doc_ptr = xmlParseDoc((const xmlChar *)input);
if (doc_ptr == NULL)
{
return 0;
}
xmlNodePtr root_element = xmlDocGetRootElement(doc_ptr);
xmlNodePtr thread_root;
if (strcmp(thread_id, "abandoned") == 0) {
if (strcmp(thread_id, "abandoned") == 0)
{
thread_root = find_child_node("abandoned", root_element);
} else {
}
else
{
xmlNodePtr threads = find_child_node("threads", root_element);
thread_root = find_child_node_with_attr("thread", "id", thread_id, threads);
}
long long total_allocated_memory =
long long total_allocated_memory =
parse_amount(get_node_text(find_child_node("total_allocated_memory", thread_root)));
long long total_mmapped_memory =
long long total_mmapped_memory =
parse_amount(get_node_text(find_child_node("total_mmapped_memory", thread_root)));
long long mmapped_regions =
long long mmapped_regions =
parse_amount(get_node_text(find_child_node("mmapped_regions", thread_root)));
xmlFreeDoc(doc_ptr);
if (total_allocated_memory == -1 || total_mmapped_memory == -1 || mmapped_regions == -1) {
if (total_allocated_memory == -1 || total_mmapped_memory == -1 || mmapped_regions == -1)
{
return 0;
}
stats->total_allocated_memory = total_allocated_memory;
......@@ -110,15 +127,17 @@ static int populate_thread_stats(const char *input, const char *thread_id, mallo
static int populate_total_free_heap_space(const char *input, long long *total_free_heap_space)
{
xmlDocPtr doc_ptr = xmlParseDoc((const xmlChar *) input);
if (doc_ptr == NULL) {
xmlDocPtr doc_ptr = xmlParseDoc((const xmlChar *)input);
if (doc_ptr == NULL)
{
return 0;
}
xmlNodePtr heap_space_root = find_child_node("total_free_heap_space", xmlDocGetRootElement(doc_ptr));
long long total_free_heap_space_parsed = parse_amount(get_node_text(heap_space_root));
xmlFreeDoc(doc_ptr);
if (total_free_heap_space_parsed == -1) {
if (total_free_heap_space_parsed == -1)
{
return 0;
}
*total_free_heap_space = total_free_heap_space_parsed;
......
......@@ -16,12 +16,14 @@
#define LIST_OVERHEAD (2*sizeof(void*))
#define OVERHEAD (2*sizeof(size_t) + sizeof(void*) + LIST_OVERHEAD)
static size_t sizes[SIZES_COUNT] = {23, 32,
256, 3072,
3584, 229376,
262144, 327680,
8 * 1024 * 1024, 16 * 1024 * 1024,
32 * 1024 * 1024};
static size_t sizes[SIZES_COUNT] = {
23, 32,
256, 3072,
3584, 229376,
262144, 327680,
8 * 1024 * 1024,16 * 1024 * 1024,
32 * 1024 * 1024
};
typedef struct {
long long mmapped_regions;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册