Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 4 additions & 6 deletions aw_scenario_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class AWScenarioRunner(object):
definition_manager = None

aw_agent = None
host_volume = os.environ["SR_HOST_VOLUME"]

def __init__(self, config: dict) -> None:
"""
Expand Down Expand Up @@ -345,11 +344,10 @@ def run(self) -> None:
scenario_process.kill()

# copy over the recording from CARLA container if env variable is setup
if self.host_volume is not None:
CARLAManager.fetch_file(
"/home/carla/recording.log",
f"{self.host_volume}/{self.results_manager.last_scenario_host}/recording.log",
)
CARLAManager.fetch_file(
"/home/carla/recording.log",
self.results_manager.last_scenario,
)

logger.info("Calculating driving score...")
driving_score = self._calculate_driving_score(result["criteria"])
Expand Down
24 changes: 23 additions & 1 deletion srunner/tools/CARLA_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,34 @@ def stop_carla():
logger.info(f"Kill stdout: {result.stdout.strip()}")
CARLAManager.container_id = None

@staticmethod
def update_permission(path: str) -> None:
"""Updates the permissions of a folder to allow non-root users to copy

Args:
path (str): file to modify
"""
env = os.environ.copy()
result = subprocess.run(
f"chmod -R 777 {path}",
shell=True,
text=True,
capture_output=True,
env=env,
)

if not result.returncode == 0:
logger.error(f"Failed to update permissions for {path}")
else:
logger.info(f"Successfully changed {path} ownership")

@staticmethod
def fetch_file(path: str, dest: str):
if CARLAManager.container_id is not None:
env = os.environ.copy()
# ensure everyone has permission to copy to dest
result = subprocess.run(
f"docker cp {CARLAManager.container_id}:{path} {dest}",
f"chmod -R 777 {dest} && docker cp {CARLAManager.container_id}:{path} {dest}",
shell=True,
text=True,
capture_output=True,
Expand Down
25 changes: 17 additions & 8 deletions srunner/tools/metrics_collector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from queue import Queue
from queue import Queue, Empty
from typing import Any

import threading
Expand Down Expand Up @@ -44,7 +44,6 @@ def reset(cls) -> None:
cls.state = {}
cls._file_target = ""
cls._thread = None
cls._running = False

@classmethod
def update_key(cls, key: str, value: Any) -> None:
Expand Down Expand Up @@ -91,22 +90,29 @@ def stop_thread(cls) -> None:

@classmethod
def _thread_target(cls) -> None:
with open(cls._file_target, "w") as f: # open the file for reading
f.write("[") # first character of json array
while cls._running:
f = open(cls._file_target, "w")

f.write("[")
while cls._running:
try:
state = cls.state_queue.get(
block=True
) # block thread until there is an object in queue
block=True, timeout=0.05
) # can't block thread as it will never finish
serialized_json = json.dumps(state)
f.write(serialized_json + ",")
f.write("]") # last json character
except Empty:
pass

f.write("]") # last json character
f.close()


if __name__ == "__main__":
# debug
target = "test_buffer.txt"

state = {"timestamp": time.perf_counter(), "value": 0}
start = time.perf_counter()

MetricsCollector.init_state(state, target, include=False)

Expand All @@ -118,4 +124,7 @@ def _thread_target(cls) -> None:
MetricsCollector.update_key("value", i)
MetricsCollector.save_state()

print(f"Took {time.perf_counter() - start} to push.")

MetricsCollector.stop_thread()
print(f"Took {time.perf_counter() - start} to join.")
6 changes: 3 additions & 3 deletions srunner/tools/results_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import json
from srunner.scenario_decoder.json_to_xml_files import XMLToFiles
from srunner.tools.CARLA_manager import CARLAManager


class ScenarioDefinitionManager(object):
Expand Down Expand Up @@ -61,10 +62,9 @@ def _create_scenario_folder(self, iteration: str, results_folder: str):

os.makedirs(full_path, exist_ok=True) # exist_ok=True, no need to error handle
self.last_scenario = full_path
self.last_scenario_host = os.path.join(
os.path.split(results_folder)[1], iteration
) # relative path on host

# update permissions
CARLAManager.update_permission(self.last_scenario)
return full_path

def parse_json(
Expand Down