Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Grpc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tci-gateway-module
Grpc
Commits
4638d7ab
Commit
4638d7ab
authored
8 years ago
by
Craig Tiller
Committed by
GitHub
8 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #9645 from ctiller/atomic_counters
Use atomics for memory counters
parents
aac5d4de
44cc814b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
test/core/util/memory_counters.c
+28
-22
28 additions, 22 deletions
test/core/util/memory_counters.c
test/core/util/memory_counters.h
+5
-5
5 additions, 5 deletions
test/core/util/memory_counters.h
with
33 additions
and
27 deletions
test/core/util/memory_counters.c
+
28
−
22
View file @
4638d7ab
...
@@ -39,7 +39,6 @@
...
@@ -39,7 +39,6 @@
#include
"test/core/util/memory_counters.h"
#include
"test/core/util/memory_counters.h"
static
gpr_mu
g_memory_mutex
;
static
struct
grpc_memory_counters
g_memory_counters
;
static
struct
grpc_memory_counters
g_memory_counters
;
static
gpr_allocation_functions
g_old_allocs
;
static
gpr_allocation_functions
g_old_allocs
;
...
@@ -50,12 +49,14 @@ static void guard_free(void *vptr);
...
@@ -50,12 +49,14 @@ static void guard_free(void *vptr);
static
void
*
guard_malloc
(
size_t
size
)
{
static
void
*
guard_malloc
(
size_t
size
)
{
size_t
*
ptr
;
size_t
*
ptr
;
if
(
!
size
)
return
NULL
;
if
(
!
size
)
return
NULL
;
gpr_mu_lock
(
&
g_memory_mutex
);
gpr_atm_no_barrier_fetch_add
(
&
g_memory_counters
.
total_size_absolute
,
g_memory_counters
.
total_size_absolute
+=
size
;
(
gpr_atm
)
size
);
g_memory_counters
.
total_size_relative
+=
size
;
gpr_atm_no_barrier_fetch_add
(
&
g_memory_counters
.
total_size_relative
,
g_memory_counters
.
total_allocs_absolute
++
;
(
gpr_atm
)
size
);
g_memory_counters
.
total_allocs_relative
++
;
gpr_atm_no_barrier_fetch_add
(
&
g_memory_counters
.
total_allocs_absolute
,
gpr_mu_unlock
(
&
g_memory_mutex
);
(
gpr_atm
)
1
);
gpr_atm_no_barrier_fetch_add
(
&
g_memory_counters
.
total_allocs_relative
,
(
gpr_atm
)
1
);
ptr
=
g_old_allocs
.
malloc_fn
(
size
+
sizeof
(
size
));
ptr
=
g_old_allocs
.
malloc_fn
(
size
+
sizeof
(
size
));
*
ptr
++
=
size
;
*
ptr
++
=
size
;
return
ptr
;
return
ptr
;
...
@@ -71,12 +72,14 @@ static void *guard_realloc(void *vptr, size_t size) {
...
@@ -71,12 +72,14 @@ static void *guard_realloc(void *vptr, size_t size) {
return
NULL
;
return
NULL
;
}
}
--
ptr
;
--
ptr
;
gpr_mu_lock
(
&
g_memory_mutex
);
gpr_atm_no_barrier_fetch_add
(
&
g_memory_counters
.
total_size_absolute
,
g_memory_counters
.
total_size_absolute
+=
size
;
(
gpr_atm
)
size
);
g_memory_counters
.
total_size_relative
-=
*
ptr
;
gpr_atm_no_barrier_fetch_add
(
&
g_memory_counters
.
total_size_relative
,
g_memory_counters
.
total_size_relative
+=
size
;
-
(
gpr_atm
)
*
ptr
);
g_memory_counters
.
total_allocs_absolute
++
;
gpr_atm_no_barrier_fetch_add
(
&
g_memory_counters
.
total_size_relative
,
gpr_mu_unlock
(
&
g_memory_mutex
);
(
gpr_atm
)
size
);
gpr_atm_no_barrier_fetch_add
(
&
g_memory_counters
.
total_allocs_absolute
,
(
gpr_atm
)
1
);
ptr
=
g_old_allocs
.
realloc_fn
(
ptr
,
size
+
sizeof
(
size
));
ptr
=
g_old_allocs
.
realloc_fn
(
ptr
,
size
+
sizeof
(
size
));
*
ptr
++
=
size
;
*
ptr
++
=
size
;
return
ptr
;
return
ptr
;
...
@@ -86,10 +89,10 @@ static void guard_free(void *vptr) {
...
@@ -86,10 +89,10 @@ static void guard_free(void *vptr) {
size_t
*
ptr
=
vptr
;
size_t
*
ptr
=
vptr
;
if
(
!
vptr
)
return
;
if
(
!
vptr
)
return
;
--
ptr
;
--
ptr
;
gpr_
mu_lock
(
&
g_memory_mutex
);
gpr_
atm_no_barrier_fetch_add
(
&
g_memory_counters
.
total_size_relative
,
g_memory_counters
.
total_size_relative
-=
*
ptr
;
-
(
gpr_atm
)
*
ptr
)
;
g_memory_counters
.
total_allocs_relative
--
;
gpr_atm_no_barrier_fetch_add
(
&
g_memory_counters
.
total_allocs_relative
,
gpr_mu_unlock
(
&
g_memory_mutex
);
-
(
gpr_atm
)
1
);
g_old_allocs
.
free_fn
(
ptr
);
g_old_allocs
.
free_fn
(
ptr
);
}
}
...
@@ -98,20 +101,23 @@ struct gpr_allocation_functions g_guard_allocs = {guard_malloc, guard_realloc,
...
@@ -98,20 +101,23 @@ struct gpr_allocation_functions g_guard_allocs = {guard_malloc, guard_realloc,
void
grpc_memory_counters_init
()
{
void
grpc_memory_counters_init
()
{
memset
(
&
g_memory_counters
,
0
,
sizeof
(
g_memory_counters
));
memset
(
&
g_memory_counters
,
0
,
sizeof
(
g_memory_counters
));
gpr_mu_init
(
&
g_memory_mutex
);
g_old_allocs
=
gpr_get_allocation_functions
();
g_old_allocs
=
gpr_get_allocation_functions
();
gpr_set_allocation_functions
(
g_guard_allocs
);
gpr_set_allocation_functions
(
g_guard_allocs
);
}
}
void
grpc_memory_counters_destroy
()
{
void
grpc_memory_counters_destroy
()
{
gpr_set_allocation_functions
(
g_old_allocs
);
gpr_set_allocation_functions
(
g_old_allocs
);
gpr_mu_destroy
(
&
g_memory_mutex
);
}
}
struct
grpc_memory_counters
grpc_memory_counters_snapshot
()
{
struct
grpc_memory_counters
grpc_memory_counters_snapshot
()
{
struct
grpc_memory_counters
counters
;
struct
grpc_memory_counters
counters
;
gpr_mu_lock
(
&
g_memory_mutex
);
counters
.
total_size_relative
=
counters
=
g_memory_counters
;
gpr_atm_no_barrier_load
(
&
g_memory_counters
.
total_size_relative
);
gpr_mu_unlock
(
&
g_memory_mutex
);
counters
.
total_size_absolute
=
gpr_atm_no_barrier_load
(
&
g_memory_counters
.
total_size_absolute
);
counters
.
total_allocs_relative
=
gpr_atm_no_barrier_load
(
&
g_memory_counters
.
total_allocs_relative
);
counters
.
total_allocs_absolute
=
gpr_atm_no_barrier_load
(
&
g_memory_counters
.
total_allocs_absolute
);
return
counters
;
return
counters
;
}
}
This diff is collapsed.
Click to expand it.
test/core/util/memory_counters.h
+
5
−
5
View file @
4638d7ab
...
@@ -34,13 +34,13 @@
...
@@ -34,13 +34,13 @@
#ifndef GRPC_TEST_CORE_UTIL_MEMORY_COUNTERS_H
#ifndef GRPC_TEST_CORE_UTIL_MEMORY_COUNTERS_H
#define GRPC_TEST_CORE_UTIL_MEMORY_COUNTERS_H
#define GRPC_TEST_CORE_UTIL_MEMORY_COUNTERS_H
#include
<
stddef
.h>
#include
<
grpc/support/atm
.h>
struct
grpc_memory_counters
{
struct
grpc_memory_counters
{
size_t
total_size_relative
;
gpr_atm
total_size_relative
;
size_t
total_size_absolute
;
gpr_atm
total_size_absolute
;
size_t
total_allocs_relative
;
gpr_atm
total_allocs_relative
;
size_t
total_allocs_absolute
;
gpr_atm
total_allocs_absolute
;
};
};
void
grpc_memory_counters_init
();
void
grpc_memory_counters_init
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment