Skip to content

Commit

Permalink
util: move all functions into util namespace (#23203)
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Dec 12, 2021
1 parent af125f3 commit 9decd3d
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 28 deletions.
14 changes: 7 additions & 7 deletions selfdrive/boardd/boardd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Panda *usb_connect(std::string serial="", uint32_t index=0) {
}

void can_send_thread(std::vector<Panda *> pandas, bool fake_send) {
set_thread_name("boardd_can_send");
util::set_thread_name("boardd_can_send");

AlignedBuffer aligned_buf;
Context * context = Context::create();
Expand Down Expand Up @@ -243,7 +243,7 @@ void can_send_thread(std::vector<Panda *> pandas, bool fake_send) {
}

void can_recv_thread(std::vector<Panda *> pandas) {
set_thread_name("boardd_can_recv");
util::set_thread_name("boardd_can_recv");

// can = 8006
PubMaster pm({"can"});
Expand Down Expand Up @@ -415,7 +415,7 @@ void send_peripheral_state(PubMaster *pm, Panda *panda) {
}

void panda_state_thread(PubMaster *pm, std::vector<Panda *> pandas, bool spoofing_started) {
set_thread_name("boardd_panda_state");
util::set_thread_name("boardd_panda_state");

Params params;
Panda *peripheral_panda = pandas[0];
Expand Down Expand Up @@ -461,7 +461,7 @@ void panda_state_thread(PubMaster *pm, std::vector<Panda *> pandas, bool spoofin


void peripheral_control_thread(Panda *panda) {
set_thread_name("boardd_peripheral_control");
util::set_thread_name("boardd_peripheral_control");

SubMaster sm({"deviceState", "driverCameraState"});

Expand Down Expand Up @@ -547,7 +547,7 @@ static void pigeon_publish_raw(PubMaster &pm, const std::string &dat) {
}

void pigeon_thread(Panda *panda) {
set_thread_name("boardd_pigeon");
util::set_thread_name("boardd_pigeon");

PubMaster pm({"ubloxRaw"});
bool ignition_last = false;
Expand Down Expand Up @@ -629,9 +629,9 @@ int main(int argc, char *argv[]) {

if (!Hardware::PC()) {
int err;
err = set_realtime_priority(54);
err = util::set_realtime_priority(54);
assert(err == 0);
err = set_core_affinity({Hardware::TICI() ? 4 : 3});
err = util::set_core_affinity({Hardware::TICI() ? 4 : 3});
assert(err == 0);
}

Expand Down
2 changes: 1 addition & 1 deletion selfdrive/camerad/cameras/camera_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ void *processing_thread(MultiCameraState *cameras, CameraState *cs, process_thre
} else {
thread_name = "WideRoadCamera";
}
set_thread_name(thread_name);
util::set_thread_name(thread_name);

uint32_t cnt = 0;
while (!do_exit) {
Expand Down
2 changes: 1 addition & 1 deletion selfdrive/camerad/cameras/camera_qcom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ static void ops_thread(MultiCameraState *s) {
CameraExpInfo road_cam_op;
CameraExpInfo driver_cam_op;

set_thread_name("camera_settings");
util::set_thread_name("camera_settings");
SubMaster sm({"sensorEvents"});
while(!do_exit) {
road_cam_op = road_cam_exp.load();
Expand Down
4 changes: 2 additions & 2 deletions selfdrive/camerad/cameras/camera_replay.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ void run_camera(CameraState *s) {
}

void road_camera_thread(CameraState *s) {
set_thread_name("replay_road_camera_thread");
util::set_thread_name("replay_road_camera_thread");
run_camera(s);
}

// void driver_camera_thread(CameraState *s) {
// set_thread_name("replay_driver_camera_thread");
// util::set_thread_name("replay_driver_camera_thread");
// run_camera(s);
// }

Expand Down
4 changes: 2 additions & 2 deletions selfdrive/camerad/cameras/camera_webcam.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void run_camera(CameraState *s, cv::VideoCapture &video_cap, float *ts) {
}

static void road_camera_thread(CameraState *s) {
set_thread_name("webcam_road_camera_thread");
util::set_thread_name("webcam_road_camera_thread");

cv::VideoCapture cap_road(ROAD_CAMERA_ID, cv::CAP_V4L2); // road
cap_road.set(cv::CAP_PROP_FRAME_WIDTH, 853);
Expand Down Expand Up @@ -186,7 +186,7 @@ void cameras_run(MultiCameraState *s) {
threads.push_back(start_process_thread(s, &s->driver_cam, process_driver_camera));

std::thread t_rear = std::thread(road_camera_thread, &s->road_cam);
set_thread_name("webcam_thread");
util::set_thread_name("webcam_thread");
driver_camera_thread(&s->driver_cam);

t_rear.join();
Expand Down
4 changes: 2 additions & 2 deletions selfdrive/camerad/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ void party(cl_device_id device_id, cl_context context) {
int main(int argc, char *argv[]) {
if (!Hardware::PC()) {
int ret;
ret = set_realtime_priority(53);
ret = util::set_realtime_priority(53);
assert(ret == 0);
ret = set_core_affinity({Hardware::EON() ? 2 : 6});
ret = util::set_core_affinity({Hardware::EON() ? 2 : 6});
assert(ret == 0 || Params().getBool("IsOffroad")); // failure ok while offroad due to offlining cores
}

Expand Down
4 changes: 2 additions & 2 deletions selfdrive/common/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <sched.h>
#endif // __linux__

namespace util {

void set_thread_name(const char* name) {
#ifdef __linux__
// pthread_setname_np is dumb (fails instead of truncates)
Expand Down Expand Up @@ -56,8 +58,6 @@ int set_core_affinity(std::vector<int> cores) {
#endif
}

namespace util {

std::string read_file(const std::string& fn) {
std::ifstream f(fn, std::ios::binary | std::ios::in);
if (f.is_open()) {
Expand Down
5 changes: 2 additions & 3 deletions selfdrive/common/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ const double MS_TO_MPH = MS_TO_KPH * KM_TO_MILE;
const double METER_TO_MILE = KM_TO_MILE / 1000.0;
const double METER_TO_FOOT = 3.28084;

void set_thread_name(const char* name);
namespace util {

void set_thread_name(const char* name);
int set_realtime_priority(int level);
int set_core_affinity(std::vector<int> cores);

namespace util {

// ***** Time helpers *****
struct tm get_time();
bool time_valid(struct tm sys_time);
Expand Down
2 changes: 1 addition & 1 deletion selfdrive/locationd/locationd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ int Localizer::locationd_thread() {
}

int main() {
set_realtime_priority(5);
util::set_realtime_priority(5);

Localizer localizer;
return localizer.locationd_thread();
Expand Down
2 changes: 1 addition & 1 deletion selfdrive/loggerd/loggerd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bool trigger_rotate_if_needed(LoggerdState *s, int cur_seg, uint32_t frame_id) {
}

void encoder_thread(LoggerdState *s, const LogCameraInfo &cam_info) {
set_thread_name(cam_info.filename);
util::set_thread_name(cam_info.filename);

int cur_seg = -1;
int encode_idx = 0;
Expand Down
4 changes: 2 additions & 2 deletions selfdrive/loggerd/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ int main(int argc, char** argv) {
setpriority(PRIO_PROCESS, 0, -20);
} else if (Hardware::TICI()) {
int ret;
ret = set_core_affinity({0, 1, 2, 3});
ret = util::set_core_affinity({0, 1, 2, 3});
assert(ret == 0);
// TODO: why does this impact camerad timings?
//ret = set_realtime_priority(1);
//ret = util::set_realtime_priority(1);
//assert(ret == 0);
}

Expand Down
8 changes: 4 additions & 4 deletions selfdrive/modeld/modeld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ mat3 cur_transform;
std::mutex transform_lock;

void calibration_thread(bool wide_camera) {
set_thread_name("calibration");
set_realtime_priority(50);
util::set_thread_name("calibration");
util::set_realtime_priority(50);

SubMaster sm({"liveCalibration"});

Expand Down Expand Up @@ -133,9 +133,9 @@ void run_model(ModelState &model, VisionIpcClient &vipc_client) {
int main(int argc, char **argv) {
if (!Hardware::PC()) {
int ret;
ret = set_realtime_priority(54);
ret = util::set_realtime_priority(54);
assert(ret == 0);
set_core_affinity({Hardware::EON() ? 2 : 7});
util::set_core_affinity({Hardware::EON() ? 2 : 7});
assert(ret == 0);
}

Expand Down

0 comments on commit 9decd3d

Please sign in to comment.