ossrs / srs

SRS is a simple, high-efficiency, real-time video server supporting RTMP, WebRTC, HLS, HTTP-FLV, SRT, MPEG-DASH, and GB28181.
https://ossrs.io
MIT License
25.15k stars 5.31k forks source link

ST: Low performance when got large amount of coroutines. #1553

Open NodeBoy2 opened 4 years ago

NodeBoy2 commented 4 years ago

Description'

Please ensure that the markdown structure is maintained.

Long running, severe memory growth, and increased close_wait on the server. Using perf to investigate, it was found that heap_delete and heap_insert are causing significant delays.

  1. SRS version: 2.0.258
  2. SRS logs:
    • Normal

Replay

How to replay bug?

Steps to reproduce the bug

1. Long-time operation/live streaming (using random values for streaming URLs, all different)

Expected behavior (Expect)

Is it because std::map<std::string, SrsSource*> SrsSource::pool is not released that causes the issue? Is there any solution available?

TRANS_BY_GPT3

NodeBoy2 commented 4 years ago

Using gdb to debug a long-running program (while streaming), in the _st_thread_create() function of the st library, the _st_active_count variable has reached 1695. At the same time, in the fetch_or_create() function of srs_app_source.cpp, the value of the pool is outputted, and it is found that the previous streaming records still exist. (gdb) p _st_active_count $1 = 1695 (gdb) p pool $1 = std::map with 845 elements = { ["/mttest/fengyifan_test"] = 0x1a1e1c0, ["/test/test_200"] = 0x29d5b20, ["origin.test.com/test/003d94d6.10c9a7116ac01061"] = 0x3621f80, ... }

TRANS_BY_GPT3

NodeBoy2 commented 4 years ago

Refer to https://github.com/ossrs/srs/issues/713. In SrsSource::fetch(), add source->die_at = -1; At the same time, add the active_at variable to SrsSource to record the last active time (update it in on_metadata, on_video, on_audio). When cleaning up SrsSource objects, first check if die_at has timed out, then check if the last active time has timed out. Can this solve the problem?

TRANS_BY_GPT3

winlinvip commented 3 years ago

The waiting performance of ST needs to be optimized.

TRANS_BY_GPT3

watchpoints commented 3 years ago

me too, CPU 20%'.

TRANS_BY_GPT3

winlinvip commented 4 months ago

There is a PR https://github.com/ossrs/state-threads/pull/5 related to this issue, improving the performance for iterating all coroutines.