Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Support new HTTP API for VALGRIND. v6.0.149 v7.0.6 #4150

Merged
merged 6 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions trunk/auto/auto_headers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ if [ $SRS_SANITIZER_LOG == YES ]; then
else
srs_undefine_macro "SRS_SANITIZER_LOG" $SRS_AUTO_HEADERS_H
fi
if [ $SRS_VALGRIND == YES ]; then
srs_define_macro "SRS_VALGRIND" $SRS_AUTO_HEADERS_H
else
srs_undefine_macro "SRS_VALGRIND" $SRS_AUTO_HEADERS_H
fi

#####################################################################################
# for embeded.
Expand Down
10 changes: 5 additions & 5 deletions trunk/src/app/srs_app_gb28181.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class SrsGbSession : public ISrsResource, public ISrsCoroutineHandler, public IS
void on_media_transport(SrsSharedResource<SrsGbMediaTcpConn> media);
// Get the candidate for SDP generation, the public IP address for device to connect to.
std::string pip();
// Interface ISrsOneCycleThreadHandler
// Interface ISrsCoroutineHandler
public:
virtual srs_error_t cycle();
private:
Expand Down Expand Up @@ -305,7 +305,7 @@ class SrsGbSipTcpConn : public ISrsResource, public ISrsCoroutineHandler, public
public:
virtual const SrsContextId& get_id();
virtual std::string desc();
// Interface ISrsOneCycleThreadHandler
// Interface ISrsCoroutineHandler
public:
virtual srs_error_t cycle();
private:
Expand Down Expand Up @@ -333,7 +333,7 @@ class SrsGbSipTcpReceiver : public ISrsStartable, public ISrsCoroutineHandler
// Interface ISrsStartable
public:
virtual srs_error_t start();
// Interface ISrsOneCycleThreadHandler
// Interface ISrsCoroutineHandler
public:
virtual srs_error_t cycle();
private:
Expand Down Expand Up @@ -362,7 +362,7 @@ class SrsGbSipTcpSender : public ISrsStartable, public ISrsCoroutineHandler
// Interface ISrsStartable
public:
virtual srs_error_t start();
// Interface ISrsOneCycleThreadHandler
// Interface ISrsCoroutineHandler
public:
virtual srs_error_t cycle();
private:
Expand Down Expand Up @@ -422,7 +422,7 @@ class SrsGbMediaTcpConn : public ISrsResource, public ISrsCoroutineHandler, publ
public:
virtual const SrsContextId& get_id();
virtual std::string desc();
// Interface ISrsOneCycleThreadHandler
// Interface ISrsCoroutineHandler
public:
virtual srs_error_t cycle();
private:
Expand Down
97 changes: 97 additions & 0 deletions trunk/src/app/srs_app_http_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ srs_error_t SrsGoApiV1::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r
urls->set("clusters", SrsJsonAny::str("origin cluster server API"));
urls->set("perf", SrsJsonAny::str("System performance stat"));
urls->set("tcmalloc", SrsJsonAny::str("tcmalloc api with params ?page=summary|api"));
urls->set("valgrind", SrsJsonAny::str("valgrind api with params ?check=full|added|changed|new|quick"));

SrsJsonObject* tests = SrsJsonAny::object();
obj->set("tests", tests);
Expand Down Expand Up @@ -1090,6 +1091,102 @@ srs_error_t SrsGoApiTcmalloc::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess
}
#endif

#ifdef SRS_VALGRIND
#include <valgrind/valgrind.h>
#include <valgrind/memcheck.h>
winlinvip marked this conversation as resolved.
Show resolved Hide resolved

SrsGoApiValgrind::SrsGoApiValgrind()
{
trd_ = NULL;
}

SrsGoApiValgrind::~SrsGoApiValgrind()
{
srs_freep(trd_);
}

srs_error_t SrsGoApiValgrind::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{
srs_error_t err = srs_success;

if (!trd_) {
trd_ = new SrsSTCoroutine("valgrind", this, _srs_context->get_id());
if ((err = trd_->start()) != srs_success) {
return srs_error_wrap(err, "start");
}
}

string check = r->query_get("check");
srs_trace("query check=%s", check.c_str());

// Must be full|added|changed|new|quick, set to full for other values.
if (check != "full" && check != "added" && check != "changed" && check != "new" && check != "quick") {
srs_warn("force set check=%s to full", check.c_str());
check = "full";
}

// By default, response the json style response.
SrsUniquePtr<SrsJsonObject> obj(SrsJsonAny::object());

obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS));

SrsJsonObject* res = SrsJsonAny::object();
res->set("check", SrsJsonAny::str(check.c_str()));
res->set("help", SrsJsonAny::str("?check=full|added|changed|new|quick"));
res->set("see", SrsJsonAny::str("https://valgrind.org/docs/manual/mc-manual.html"));
obj->set("data", res);

// Does a memory check later.
if (check == "full") {
res->set("call", SrsJsonAny::str("VALGRIND_DO_LEAK_CHECK"));
} else if (check == "quick") {
res->set("call", SrsJsonAny::str("VALGRIND_DO_QUICK_LEAK_CHECK"));
} else if (check == "added") {
res->set("call", SrsJsonAny::str("VALGRIND_DO_ADDED_LEAK_CHECK"));
} else if (check == "changed") {
res->set("call", SrsJsonAny::str("VALGRIND_DO_CHANGED_LEAK_CHECK"));
} else if (check == "new") {
res->set("call", SrsJsonAny::str("VALGRIND_DO_NEW_LEAK_CHECK"));
}
task_ = check;

return srs_api_response(w, r, obj->dumps());
}

srs_error_t SrsGoApiValgrind::cycle()
{
srs_error_t err = srs_success;

while (true) {
if ((err = trd_->pull()) != srs_success) {
return srs_error_wrap(err, "pull");
}

std::string check = task_;
task_ = "";

if (!check.empty()) {
srs_trace("do memory check=%s", check.c_str());
}

if (check == "full") {
VALGRIND_DO_LEAK_CHECK;
} else if (check == "quick") {
VALGRIND_DO_QUICK_LEAK_CHECK;
} else if (check == "added") {
VALGRIND_DO_ADDED_LEAK_CHECK;
} else if (check == "changed") {
VALGRIND_DO_CHANGED_LEAK_CHECK;
} else if (check == "new") {
VALGRIND_DO_NEW_LEAK_CHECK;
}

srs_usleep(3 * SRS_UTIME_SECONDS);
}

return err;
}
#endif

SrsGoApiMetrics::SrsGoApiMetrics()
{
Expand Down
17 changes: 17 additions & 0 deletions trunk/src/app/srs_app_http_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,23 @@ class SrsGoApiTcmalloc : public ISrsHttpHandler
};
#endif

#ifdef SRS_VALGRIND
class SrsGoApiValgrind : public ISrsHttpHandler, public ISrsCoroutineHandler
{
private:
SrsCoroutine* trd_;
std::string task_;
public:
SrsGoApiValgrind();
virtual ~SrsGoApiValgrind();
public:
virtual srs_error_t serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
// Interface ISrsCoroutineHandler
public:
virtual srs_error_t cycle();
};
#endif

class SrsGoApiMetrics : public ISrsHttpHandler
{
private:
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_http_conn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class SrsHttpConn : public ISrsConnection, public ISrsStartable, public ISrsCoro
// Interface ISrsStartable
public:
virtual srs_error_t start();
// Interface ISrsOneCycleThreadHandler
// Interface ISrsCoroutineHandler
public:
virtual srs_error_t cycle();
private:
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_recv_thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class SrsHttpRecvThread : public ISrsCoroutineHandler
virtual srs_error_t start();
public:
virtual srs_error_t pull();
// Interface ISrsOneCycleThreadHandler
// Interface ISrsCoroutineHandler
public:
virtual srs_error_t cycle();
};
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_rtmp_conn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class SrsRtmpConn : public ISrsConnection, public ISrsStartable, public ISrsRelo
// when client cycle thread stop, invoke the on_thread_stop(), which will use server
// To remove the client by server->remove(this).
virtual srs_error_t start();
// Interface ISrsOneCycleThreadHandler
// Interface ISrsCoroutineHandler
public:
// The thread cycle function,
// when serve connection completed, terminate the loop which will terminate the thread,
Expand Down
10 changes: 9 additions & 1 deletion trunk/src/app/srs_app_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,17 @@ srs_error_t SrsServer::http_handle()
// The test api for get tcmalloc stats.
// @see Memory Introspection in https://gperftools.github.io/gperftools/tcmalloc.html
if ((err = http_api_mux->handle("/api/v1/tcmalloc", new SrsGoApiTcmalloc())) != srs_success) {
return srs_error_wrap(err, "handle tests errors");
return srs_error_wrap(err, "handle tcmalloc errors");
}
#endif

#ifdef SRS_VALGRIND
// The test api for valgrind. See VALGRIND_DO_LEAK_CHECK in https://valgrind.org/docs/manual/mc-manual.html
if ((err = http_api_mux->handle("/api/v1/valgrind", new SrsGoApiValgrind())) != srs_success) {
return srs_error_wrap(err, "handle valgrind errors");
}
#endif

// metrics by prometheus
if ((err = http_api_mux->handle("/metrics", new SrsGoApiMetrics())) != srs_success) {
return srs_error_wrap(err, "handle tests errors");
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_st.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class SrsExecutorCoroutine : public ISrsResource, public ISrsStartable, public I
public:
virtual const SrsContextId& cid();
virtual void set_cid(const SrsContextId& cid);
// Interface ISrsOneCycleThreadHandler
// Interface ISrsCoroutineHandler
public:
virtual srs_error_t cycle();
// Interface ISrsResource
Expand Down
Loading