Skip to content

Commit

Permalink
API: Support new HTTP API for VALGRIND. v6.0.149 v7.0.6 (#4150)
Browse files Browse the repository at this point in the history
New features for valgrind:

1. ST: Support /api/v1/valgrind for leaking check.
2. ST: Support /api/v1/valgrind?check=full|added|changed|new|quick

To use Valgrind to detect memory leaks in SRS, even though Valgrind
hooks are supported in ST, there are still many false positives. A more
reasonable approach is to have Valgrind report incremental memory leaks.
This way, global and static variables can be avoided, and detection can
be achieved without exiting the program. Follow these steps:

1. Compile SRS with Valgrind support: `./configure --valgrind=on &&
make`
2. Start SRS with memory leak detection enabled: `valgrind
--leak-check=full ./objs/srs -c conf/console.conf`
3. Trigger memory detection by using curl to access the API and generate
calibration data. There will still be many false positives, but these
can be ignored: `curl http://127.0.0.1:1985/api/v1/valgrind?check=added`
4. Perform load testing or test the suspected leaking functionality,
such as RTMP streaming: `ffmpeg -re -i doc/source.flv -c copy -f flv
rtmp://127.0.0.1/live/livestream`
5. Stop streaming and wait for SRS to clean up the Source memory,
approximately 30 seconds.
6. Perform incremental memory leak detection. The reported leaks will be
very accurate at this point: `curl
http://127.0.0.1:1985/api/v1/valgrind?check=added`

> Note: To avoid interference from the HTTP request itself on Valgrind,
SRS uses a separate coroutine to perform periodic checks. Therefore,
after accessing the API, you may need to wait a few seconds for the
detection to be triggered.

---------

Co-authored-by: Jacob Su <suzp1984@gmail.com>
  • Loading branch information
winlinvip and suzp1984 authored Aug 21, 2024
1 parent 3e811ba commit 0d76081
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 12 deletions.
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
2 changes: 2 additions & 0 deletions trunk/doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The changelog for SRS.
<a name="v7-changes"></a>

## SRS 7.0 Changelog
* v7.0, 2024-08-21, Merge [#4150](https://github.com/ossrs/srs/pull/4150): API: Support new HTTP API for VALGRIND. v7.0.6 (#4150)
* v7.0, 2024-08-15, Merge [#4144](https://github.com/ossrs/srs/pull/4144): HTTP-FLV: Crash when multiple viewers. v7.0.5 (#4144)
* v7.0, 2024-08-15, Merge [#4142](https://github.com/ossrs/srs/pull/4142): Config: Add more utest for env config. v7.0.4 (#4142)
* v7.0, 2024-08-14, Merge [#4141](https://github.com/ossrs/srs/pull/4141): Live: Crash for invalid live stream state when unmount HTTP. v7.0.3 (#4141)
Expand All @@ -17,6 +18,7 @@ The changelog for SRS.
<a name="v6-changes"></a>

## SRS 6.0 Changelog
* v6.0, 2024-08-21, Merge [#4150](https://github.com/ossrs/srs/pull/4150): API: Support new HTTP API for VALGRIND. v6.0.149 (#4150)
* v6.0, 2024-08-15, Merge [#4144](https://github.com/ossrs/srs/pull/4144): HTTP-FLV: Crash when multiple viewers. v6.0.148 (#4144)
* v6.0, 2024-08-15, Merge [#4142](https://github.com/ossrs/srs/pull/4142): Config: Add more utest for env config. v6.0.147 (#4142)
* v6.0, 2024-08-14, Merge [#4141](https://github.com/ossrs/srs/pull/4141): Live: Crash for invalid live stream state when unmount HTTP. v6.0.146 (#4141)
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
100 changes: 100 additions & 0 deletions trunk/src/app/srs_app_http_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ using namespace std;
#include <srs_protocol_utility.hpp>
#include <srs_app_coworkers.hpp>

#ifdef SRS_VALGRIND
#include <valgrind/valgrind.h>
#include <valgrind/memcheck.h>
#endif

#if defined(__linux__) || defined(SRS_OSX)
#include <sys/utsname.h>
#endif
Expand Down Expand Up @@ -267,6 +272,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 +1096,100 @@ srs_error_t SrsGoApiTcmalloc::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess
}
#endif

#ifdef SRS_VALGRIND

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
2 changes: 1 addition & 1 deletion trunk/src/core/srs_core_version6.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

#define VERSION_MAJOR 6
#define VERSION_MINOR 0
#define VERSION_REVISION 148
#define VERSION_REVISION 149

#endif
2 changes: 1 addition & 1 deletion trunk/src/core/srs_core_version7.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

#define VERSION_MAJOR 7
#define VERSION_MINOR 0
#define VERSION_REVISION 5
#define VERSION_REVISION 6

#endif

0 comments on commit 0d76081

Please sign in to comment.