Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
615f6923
Commit
615f6923
authored
Dec 17, 2014
by
antirez
Browse files
getMemorySize() moved into zmalloc.c with other low level mem utils.
See issue #2218.
parent
e1e41f69
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/redis.c
View file @
615f6923
...
...
@@ -289,7 +289,6 @@ struct redisCommand redisCommandTable[] = {
};
struct
evictionPoolEntry
*
evictionPoolAlloc
(
void
);
static
size_t
getMemorySize
(
void
);
/*============================ Utility functions ============================ */
...
...
@@ -1761,7 +1760,7 @@ void initServer(void) {
server
.
clients_waiting_acks
=
listCreate
();
server
.
get_ack_from_slaves
=
0
;
server
.
clients_paused
=
0
;
server
.
system_memory_size
=
get
M
emory
S
ize
();
server
.
system_memory_size
=
zmalloc_
get
_m
emory
_s
ize
();
createSharedObjects
();
adjustOpenFilesLimit
();
...
...
@@ -2572,54 +2571,6 @@ void commandCommand(redisClient *c) {
}
}
/**
* Returns the size of physical memory (RAM) in bytes.
* It looks ugly, but this is the cleanest way to achive cross platform results.
* Cleaned up from:
* http://nadeausoftware.com/articles/2012/09/c_c_tip_how_get_physical_memory_size_system
*/
size_t
getMemorySize
()
{
#if defined(__unix__) || defined(__unix) || defined(unix) || \
(defined(__APPLE__) && defined(__MACH__))
#if defined(CTL_HW) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64))
int
mib
[
2
];
mib
[
0
]
=
CTL_HW
;
#if defined(HW_MEMSIZE)
mib
[
1
]
=
HW_MEMSIZE
;
/* OSX. --------------------- */
#elif defined(HW_PHYSMEM64)
mib
[
1
]
=
HW_PHYSMEM64
;
/* NetBSD, OpenBSD. --------- */
#endif
int64_t
size
=
0
;
/* 64-bit */
size_t
len
=
sizeof
(
size
);
if
(
sysctl
(
mib
,
2
,
&
size
,
&
len
,
NULL
,
0
)
==
0
)
return
(
size_t
)
size
;
return
0L
;
/* Failed? */
#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
/* FreeBSD, Linux, OpenBSD, and Solaris. -------------------- */
return
(
size_t
)
sysconf
(
_SC_PHYS_PAGES
)
*
(
size_t
)
sysconf
(
_SC_PAGESIZE
);
#elif defined(CTL_HW) && (defined(HW_PHYSMEM) || defined(HW_REALMEM))
/* DragonFly BSD, FreeBSD, NetBSD, OpenBSD, and OSX. -------- */
int
mib
[
2
];
mib
[
0
]
=
CTL_HW
;
#if defined(HW_REALMEM)
mib
[
1
]
=
HW_REALMEM
;
/* FreeBSD. ----------------- */
#elif defined(HW_PYSMEM)
mib
[
1
]
=
HW_PHYSMEM
;
/* Others. ------------------ */
#endif
unsigned
int
size
=
0
;
/* 32-bit */
size_t
len
=
sizeof
(
size
);
if
(
sysctl
(
mib
,
2
,
&
size
,
&
len
,
NULL
,
0
)
==
0
)
return
(
size_t
)
size
;
return
0L
;
/* Failed? */
#endif
/* sysctl and sysconf variants */
#else
return
0L
;
/* Unknown OS. */
#endif
}
/* Convert an amount of bytes into a human readable string in the form
* of 100B, 2G, 100M, 4K, and so forth. */
void
bytesToHuman
(
char
*
s
,
unsigned
long
long
n
)
{
...
...
src/zmalloc.c
View file @
615f6923
...
...
@@ -364,3 +364,52 @@ size_t zmalloc_get_smap_bytes_by_field(char *field) {
size_t
zmalloc_get_private_dirty
(
void
)
{
return
zmalloc_get_smap_bytes_by_field
(
"Private_Dirty:"
);
}
/* Returns the size of physical memory (RAM) in bytes.
* It looks ugly, but this is the cleanest way to achive cross platform results.
* Cleaned up from:
* http://nadeausoftware.com/articles/2012/09/c_c_tip_how_get_physical_memory_size_system
*/
size_t
zmalloc_get_memory_size
(
void
)
{
#if defined(__unix__) || defined(__unix) || defined(unix) || \
(defined(__APPLE__) && defined(__MACH__))
#if defined(CTL_HW) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64))
int
mib
[
2
];
mib
[
0
]
=
CTL_HW
;
#if defined(HW_MEMSIZE)
mib
[
1
]
=
HW_MEMSIZE
;
/* OSX. --------------------- */
#elif defined(HW_PHYSMEM64)
mib
[
1
]
=
HW_PHYSMEM64
;
/* NetBSD, OpenBSD. --------- */
#endif
int64_t
size
=
0
;
/* 64-bit */
size_t
len
=
sizeof
(
size
);
if
(
sysctl
(
mib
,
2
,
&
size
,
&
len
,
NULL
,
0
)
==
0
)
return
(
size_t
)
size
;
return
0L
;
/* Failed? */
#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
/* FreeBSD, Linux, OpenBSD, and Solaris. -------------------- */
return
(
size_t
)
sysconf
(
_SC_PHYS_PAGES
)
*
(
size_t
)
sysconf
(
_SC_PAGESIZE
);
#elif defined(CTL_HW) && (defined(HW_PHYSMEM) || defined(HW_REALMEM))
/* DragonFly BSD, FreeBSD, NetBSD, OpenBSD, and OSX. -------- */
int
mib
[
2
];
mib
[
0
]
=
CTL_HW
;
#if defined(HW_REALMEM)
mib
[
1
]
=
HW_REALMEM
;
/* FreeBSD. ----------------- */
#elif defined(HW_PYSMEM)
mib
[
1
]
=
HW_PHYSMEM
;
/* Others. ------------------ */
#endif
unsigned
int
size
=
0
;
/* 32-bit */
size_t
len
=
sizeof
(
size
);
if
(
sysctl
(
mib
,
2
,
&
size
,
&
len
,
NULL
,
0
)
==
0
)
return
(
size_t
)
size
;
return
0L
;
/* Failed? */
#endif
/* sysctl and sysconf variants */
#else
return
0L
;
/* Unknown OS. */
#endif
}
src/zmalloc.h
View file @
615f6923
...
...
@@ -77,6 +77,7 @@ float zmalloc_get_fragmentation_ratio(size_t rss);
size_t
zmalloc_get_rss
(
void
);
size_t
zmalloc_get_private_dirty
(
void
);
size_t
zmalloc_get_smap_bytes_by_field
(
char
*
field
);
size_t
zmalloc_get_memory_size
(
void
);
void
zlibc_free
(
void
*
ptr
);
#ifndef HAVE_MALLOC_SIZE
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment