Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Enable heap prof of stmgr in heron-shell. #2005

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
9 changes: 8 additions & 1 deletion heron/executor/src/python/heron_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ def start_process_monitor(self):
Log.info("%s exited too many times" % name)
sys.exit(1)
time.sleep(self.interval_between_runs)
p = self._run_process(name, command)
p = self._run_process(name, command, self.shell_env)
del self.processes_to_monitor[pid]
self.processes_to_monitor[p.pid] =\
ProcessInfo(p, name, command, old_process_info.attempts + 1)
Expand Down Expand Up @@ -892,6 +892,13 @@ def main():
# PEX_ROOT shell environment before forking the processes
shell_env = os.environ.copy()
shell_env["PEX_ROOT"] = os.path.join(os.path.abspath('.'), ".pex")
# Refer to https://gperftools.github.io/gperftools/heapprofile.html
# for details of settings of gperftools heap profiler
shell_env["HEAPPROFILE"] = "stmgr.hprof"
shell_env["HEAP_PROFILE_ALLOCATION_INTERVAL"] = "0"
shell_env["HEAP_PROFILE_INUSE_INTERVAL"] = "0"
shell_env["HEAPPROFILESIGNAL"] = str(signal.SIGUSR1)


# Instantiate the executor, bind it to signal handlers and launch it
executor = HeronExecutor(sys.argv, shell_env)
Expand Down
1 change: 1 addition & 0 deletions heron/shell/src/python/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
from jstackhandler import JstackHandler
from memoryhistogramhandler import MemoryHistogramHandler
from pidhandler import PidHandler
from stmgrheapprofhandler import StmgrHeapProfHandler
45 changes: 45 additions & 0 deletions heron/shell/src/python/handlers/stmgrheapprofhandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2017 Twitter. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

''' stmgrheapprofhandler.py '''
import glob
import json
import os
import signal
import tornado.web

from heron.shell.src.python import utils

class StmgrHeapProfHandler(tornado.web.RequestHandler):
"""
Responsible for getting the process ID for an instance.
"""

# pylint: disable=attribute-defined-outside-init
@tornado.web.asynchronous
def get(self):
''' get method '''
self.content_type = 'application/json'
stmgr_pid_files = glob.glob('stmgr*.pid')
try:
pid_file = stmgr_pid_files[0]
with open(pid_file, 'r') as f:
pid = f.read()
os.kill(int(pid), signal.SIGUSR1)
self.write('Performing heap profiling on stream manager...')
self.finish()
except:
self.write("Not stream manager found")
self.set_status(404)
self.finish()
1 change: 1 addition & 0 deletions heron/shell/src/python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
(r"^/filedata/(.*)", handlers.FileDataHandler),
(r"^/filestats/(.*)", handlers.FileStatsHandler),
(r"^/download/(.*)", handlers.DownloadHandler),
(r"^/stmgrheapprof", handlers.StmgrHeapProfHandler),
])


Expand Down
2 changes: 2 additions & 0 deletions heron/stmgr/src/cpp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ cc_binary(
"server/stmgr-main.cpp",
],
copts = [
"-Ithird_party",
"-Iheron",
"-Iheron/common/src/cpp",
"-Iheron/statemgrs/src/cpp",
Expand All @@ -129,6 +130,7 @@ cc_binary(
"//heron/common/src/cpp/metrics:metrics-cxx",
"//heron/statemgrs/src/cpp:statemgrs-cxx",
"//third_party/yaml-cpp:yaml-cxx",
"//third_party/gperftools:profiler-cxx",
],
linkstatic = 1,
)
4 changes: 4 additions & 0 deletions heron/stmgr/src/cpp/server/stmgr-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

#include <gperftools/heap-profiler.h>
#include <iostream>
#include <string>
#include <vector>
Expand Down Expand Up @@ -55,6 +56,8 @@ int main(int argc, char* argv[]) {
sp_int32 shell_port = atoi(argv[11]);
sp_string heron_internals_config_filename = argv[12];

HeapProfilerStart("stmgr");
Copy link
Contributor

@huijunw huijunw Jun 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The profiling is enabled during the whole stmgr life time.

What about adding two handlers to the stmgr server to control the profiling start & end:

  1. HandleStartHeapProfilingMessage: HeapProfilerStart()
  2. HandleStopHeapProfilingMessage: HeapProfilerStop()

In this way, the profiling is not enabled by default and there is no impact on performance.


EventLoopImpl ss;

// Read heron internals config from local file
Expand Down Expand Up @@ -82,5 +85,6 @@ int main(int argc, char* argv[]) {
high_watermark, low_watermark);
mgr.Init();
ss.loop();
HeapProfilerStop();
return 0;
}
1 change: 1 addition & 0 deletions third_party/gperftools/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ cc_library(
"lib/libprofiler.a",
],
hdrs = [
"include/gperftools/heap-profiler.h",
"include/gperftools/profiler.h",
],
includes = ["include"],
Expand Down