Commit 74e853aa authored by wangxuefeng's avatar wangxuefeng

Sync from brpc office repo.

parent 4cd5a7fe
......@@ -63,15 +63,25 @@
#include "brpc/socket_map.h" // SocketMapList
#include "brpc/server.h"
#include "brpc/trackme.h" // TrackMe
#include <malloc.h> // malloc_trim
#include "brpc/details/usercode_backup_pool.h"
#include <malloc.h> // malloc_trim
#include "butil/fd_guard.h"
#include "butil/files/file_watcher.h"
extern "C" {
// defined in gperftools/malloc_extension_c.h
void BAIDU_WEAK MallocExtension_ReleaseFreeMemory(void);
}
namespace brpc {
DECLARE_bool(usercode_in_pthread);
DEFINE_int32(free_memory_to_system_interval, 0,
"Try to return free memory to system every so many seconds, "
"values <= 0 disables this feature");
BRPC_VALIDATE_GFLAG(free_memory_to_system_interval, PassValidate);
namespace policy {
// Defined in http_rpc_protocol.cpp
void InitCommonStrings();
......@@ -178,7 +188,7 @@ static void* GlobalUpdate(void*) {
const int WARN_NOSLEEP_THRESHOLD = 2;
int64_t last_time_us = start_time_us;
int consecutive_nosleep = 0;
//int64_t last_malloc_trim_time = start_time_us;
int64_t last_return_free_memory_time = start_time_us;
while (1) {
const int64_t sleep_us = 1000000L + last_time_us - butil::gettimeofday_us();
if (sleep_us > 0) {
......@@ -215,11 +225,24 @@ static void* GlobalUpdate(void*) {
}
}
// TODO: Add branch for tcmalloc.
// if (last_time_us > last_malloc_trim_time + 10*1000000L) {
// last_malloc_trim_time = last_time_us;
// malloc_trim(10*1024*1024/*leave 10M pad*/);
// }
const int return_mem_interval =
FLAGS_free_memory_to_system_interval/*reloadable*/;
if (return_mem_interval > 0 &&
last_time_us >= last_return_free_memory_time +
return_mem_interval * 1000000L) {
last_return_free_memory_time = last_time_us;
// TODO: Calling MallocExtension::instance()->ReleaseFreeMemory may
// crash the program in later calls to malloc, verified on tcmalloc
// 1.7 and 2.5, which means making the static member function weak
// in details/tcmalloc_extension.cpp is probably not correct, however
// it does work for heap profilers.
if (MallocExtension_ReleaseFreeMemory != NULL) {
MallocExtension_ReleaseFreeMemory();
} else {
// GNU specific.
malloc_trim(10 * 1024 * 1024/*leave 10M pad*/);
}
}
}
return NULL;
}
......
......@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Authors: Ge,Jun (gejun@baidu.com)
// Authors: wangxuefeng (wangxuefeng@didichuxing.com)
#include <google/protobuf/descriptor.h> // MethodDescriptor
#include <google/protobuf/message.h> // Message
......
......@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Authors: Ge,Jun (gejun@baidu.com)
// Authors: wangxuefeng (wangxuefeng@didichuxing.com)
#ifndef BRPC_POLICY_THRIFT_PROTOCOL_H
#define BRPC_POLICY_THRIFT_PROTOCOL_H
......
......@@ -167,10 +167,6 @@ const ConnectionType CONNECTION_TYPE_ALL =
(int)CONNECTION_TYPE_POOLED |
(int)CONNECTION_TYPE_SHORT);
// DEPRECATED: old names.
const ProtocolType PROTOCOL_BAIDU_RPC = PROTOCOL_BAIDU_STD;
const ProtocolType PROTOCOL_MEMCACHE_BINARY = PROTOCOL_MEMCACHE;
// [thread-safe]
// Register `protocol' using key=`type'.
// Returns 0 on success, -1 otherwise
......
......@@ -210,30 +210,15 @@ static void PrintSupportedCompressions(std::ostream& os, void*) {
}
}
static bool check_TCMALLOC_SAMPLE_PARAMETER() {
char* str = getenv("TCMALLOC_SAMPLE_PARAMETER");
if (str == NULL) {
return false;
}
char* endptr;
int val = strtol(str, &endptr, 10);
return (*endptr == '\0' && val > 0);
}
static bool has_TCMALLOC_SAMPLE_PARAMETER() {
static bool val = check_TCMALLOC_SAMPLE_PARAMETER();
return val;
}
static void PrintEnabledProfilers(std::ostream& os, void*) {
if (cpu_profiler_enabled) {
os << "cpu ";
}
if (IsHeapProfilerEnabled) {
if (IsHeapProfilerEnabled()) {
if (has_TCMALLOC_SAMPLE_PARAMETER()) {
os << "heap ";
} else {
os << "heap(lack of TCMALLOC_SAMPLE_PARAMETER) ";
os << "heap(no TCMALLOC_SAMPLE_PARAMETER in env) ";
}
}
os << "contention";
......@@ -670,6 +655,15 @@ struct RevertServerStatus {
}
};
static int get_port_from_fd(int fd) {
struct sockaddr_in addr;
socklen_t size = sizeof(addr);
if (getsockname(fd, (struct sockaddr*)&addr, &size) < 0) {
return -1;
}
return ntohs(addr.sin_port);
}
int Server::StartInternal(const butil::ip_t& ip,
const PortRange& port_range,
const ServerOptions *opt) {
......@@ -901,6 +895,15 @@ int Server::StartInternal(const butil::ip_t& ip,
}
return -1;
}
if (_listen_addr.port == 0) {
// port=0 makes kernel dynamically select a port from
// https://en.wikipedia.org/wiki/Ephemeral_port
_listen_addr.port = get_port_from_fd(sockfd);
if (_listen_addr.port <= 0) {
LOG(ERROR) << "Fail to get port from fd=" << sockfd;
return -1;
}
}
if (_am == NULL) {
_am = BuildAcceptor();
if (NULL == _am) {
......@@ -930,6 +933,12 @@ int Server::StartInternal(const butil::ip_t& ip,
<< " is same with port=" << _listen_addr.port << " to Start()";
return -1;
}
if (_options.internal_port == 0) {
LOG(ERROR) << "ServerOptions.internal_port cannot be 0, which"
" allocates a dynamic and probabaly unfiltered port,"
" against the purpose of \"being internal\".";
return -1;
}
butil::EndPoint internal_point = _listen_addr;
internal_point.port = _options.internal_port;
butil::fd_guard sockfd(tcp_listen(internal_point, FLAGS_reuse_addr));
......
......@@ -247,7 +247,7 @@ struct ServerOptions {
// Provide builtin services at this port rather than the port to Start().
// When your server needs to be accessed from public (including traffic
// redirected by nginx or other http front-end servers), set this port
// to a port number that's ONLY accessible from Baidu's internal network
// to a port number that's ONLY accessible from internal network
// so that you can check out the builtin services from this port while
// hiding them from public. Setting this option also enables security
// protection code which we may add constantly.
......@@ -415,28 +415,25 @@ public:
Server(ProfilerLinker = ProfilerLinker());
~Server();
// Start this server. Use default options if `opt' is NULL.
// This function can be called multiple times if the server is completely
// stopped by Stop() and Join().
// A set of functions to start this server.
// Returns 0 on success, -1 otherwise and errno is set appropriately.
// Notes:
// * Default options are taken if `opt' is NULL.
// * A server can be started more than once if the server is completely
// stopped by Stop() and Join().
// * port can be 0, which makes kernel to choose a port dynamically.
// Start on a single address "0.0.0.0:8000".
// Start on an address in form of "0.0.0.0:8000".
int Start(const char* ip_port_str, const ServerOptions* opt);
int Start(const butil::EndPoint& ip_port, const ServerOptions* opt);
// Start on IP_ANY:port.
int Start(int port, const ServerOptions* opt);
// Start on ip:port enclosed in butil::EndPoint which is defined in
// src/butil/endpoint.h
int Start(const butil::EndPoint& ip_port, const ServerOptions* opt);
// Start on `ip_str' + any useable port in `port_range'
int Start(const char* ip_str, PortRange port_range,
const ServerOptions *opt);
// Start on `ip_str' + any useable port in `range'
int Start(const char* ip_str, PortRange range, const ServerOptions *opt);
// NOTE: Stop() is paired with Join() to stop a server with minimum lost
// of requests. The point of separating them is that you can Stop()
// multiple servers before Join()-ing them, the total time to Join is
// NOTE: Stop() is paired with Join() to stop a server without losing
// requests. The point of separating them is that you can Stop() multiple
// servers before Join() them, in which case the total time to Join is
// time of the slowest Join(). Otherwise you have to Join() them one by
// one, in which case the total time is sum of all Join().
......
......@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Authors: Ge,Jun (gejun@baidu.com)
// Authors: wangxuefeng (wangxuefeng@didichuxing.com)
#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
#include "brpc/thrift_binary_message.h"
......
......@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Authors: Ge,Jun (gejun@baidu.com)
// Authors: wangxuefeng (wangxuefeng@didichuxing.com)
#ifndef BRPC_THRIFT_BINARY_MESSAGE_H
#define BRPC_THRIFT_BINARY_MESSAGE_H
......
......@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Authors: Ge,Jun (gejun@baidu.com)
// Authors: wangxuefeng (wangxuefeng@didichuxing.com)
#include "butil/class_name.h"
#include "brpc/thrift_service.h"
......
......@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Authors: Ge,Jun (gejun@baidu.com)
// Authors: wangxuefeng (wangxuefeng@didichuxing.com)
#ifndef BRPC_THRIFT_SERVICE_H
#define BRPC_THRIFT_SERVICE_H
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment