forked from platformio/platformio-core
Implement memusage.history RPC
This commit is contained in:
@ -13,6 +13,7 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
import functools
|
import functools
|
||||||
|
import os
|
||||||
|
|
||||||
from platformio.home.rpc.handlers.base import BaseRPCHandler
|
from platformio.home.rpc.handlers.base import BaseRPCHandler
|
||||||
from platformio.project import memusage
|
from platformio.project import memusage
|
||||||
@ -21,27 +22,35 @@ from platformio.project import memusage
|
|||||||
class MemUsageRPC(BaseRPCHandler):
|
class MemUsageRPC(BaseRPCHandler):
|
||||||
NAMESPACE = "memusage"
|
NAMESPACE = "memusage"
|
||||||
|
|
||||||
async def summary(self, project_dir, env, options=None):
|
async def profile(self, project_dir, env, options=None):
|
||||||
options = options or {}
|
options = options or {}
|
||||||
existing_reports = memusage.list_reports(project_dir, env)
|
report_dir = memusage.get_report_dir(project_dir, env)
|
||||||
current_report = previous_report = None
|
if options.get("lazy"):
|
||||||
if options.get("cached") and existing_reports:
|
existing_reports = memusage.list_reports(report_dir)
|
||||||
current_report = memusage.read_report(existing_reports[-1])
|
|
||||||
if len(existing_reports) > 1:
|
|
||||||
previous_report = memusage.read_report(existing_reports[-2])
|
|
||||||
else:
|
|
||||||
if existing_reports:
|
if existing_reports:
|
||||||
previous_report = memusage.read_report(existing_reports[-1])
|
return existing_reports[-1]
|
||||||
await self.factory.manager.dispatcher["core.exec"](
|
await self.factory.manager.dispatcher["core.exec"](
|
||||||
["run", "-d", project_dir, "-e", env, "-t", "__memusage"],
|
["run", "-d", project_dir, "-e", env, "-t", "__memusage"],
|
||||||
options=options.get("exec"),
|
options=options.get("exec"),
|
||||||
raise_exception=True,
|
raise_exception=True,
|
||||||
)
|
)
|
||||||
current_report = memusage.read_report(
|
return memusage.list_reports(report_dir)[-1]
|
||||||
memusage.list_reports(project_dir, env)[-1]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
def summary(self, report_path):
|
||||||
max_top_items = 10
|
max_top_items = 10
|
||||||
|
report_dir = os.path.dirname(report_path)
|
||||||
|
existing_reports = memusage.list_reports(report_dir)
|
||||||
|
current_report = memusage.read_report(report_path)
|
||||||
|
previous_report = None
|
||||||
|
try:
|
||||||
|
current_index = existing_reports.index(report_path)
|
||||||
|
if current_index > 0:
|
||||||
|
previous_report = memusage.read_report(
|
||||||
|
existing_reports[current_index - 1]
|
||||||
|
)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
return dict(
|
return dict(
|
||||||
timestamp=dict(
|
timestamp=dict(
|
||||||
current=current_report["timestamp"],
|
current=current_report["timestamp"],
|
||||||
@ -62,7 +71,7 @@ class MemUsageRPC(BaseRPCHandler):
|
|||||||
0:max_top_items
|
0:max_top_items
|
||||||
],
|
],
|
||||||
sections=sorted(
|
sections=sorted(
|
||||||
current_report["memory"]["total"]["sections"].values(),
|
current_report["memory"]["sections"].values(),
|
||||||
key=lambda item: item["size"],
|
key=lambda item: item["size"],
|
||||||
reverse=True,
|
reverse=True,
|
||||||
)[0:max_top_items],
|
)[0:max_top_items],
|
||||||
@ -98,3 +107,18 @@ class MemUsageRPC(BaseRPCHandler):
|
|||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
return sorted(symbols, key=lambda item: item["size"], reverse=True)
|
return sorted(symbols, key=lambda item: item["size"], reverse=True)
|
||||||
|
|
||||||
|
async def history(self, project_dir, env, nums=10):
|
||||||
|
result = []
|
||||||
|
report_dir = memusage.get_report_dir(project_dir, env)
|
||||||
|
reports = memusage.list_reports(report_dir)[nums * -1 :]
|
||||||
|
for path in reports:
|
||||||
|
data = memusage.read_report(path)
|
||||||
|
result.append(
|
||||||
|
{
|
||||||
|
"timestamp": data["timestamp"],
|
||||||
|
"ram": data["memory"]["total"]["ram_size"],
|
||||||
|
"flash": data["memory"]["total"]["flash_size"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return result
|
||||||
|
@ -28,8 +28,7 @@ def get_report_dir(project_dir, env):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def list_reports(project_dir, env):
|
def list_reports(report_dir):
|
||||||
report_dir = get_report_dir(project_dir, env)
|
|
||||||
if not os.path.isdir(report_dir):
|
if not os.path.isdir(report_dir):
|
||||||
return []
|
return []
|
||||||
return [os.path.join(report_dir, item) for item in sorted(os.listdir(report_dir))]
|
return [os.path.join(report_dir, item) for item in sorted(os.listdir(report_dir))]
|
||||||
|
Reference in New Issue
Block a user