util.cc: reduce two read_file functions into one (#20655)

* no need to malloc one extra byte

* combine two read_file into a faster one

* cleanup #include

* use resize

* apply suggestions from review

* space

* rebase master
albatross
Dean Lee 2021-04-14 02:43:43 +08:00 committed by GitHub
parent eb4666b38f
commit fe2f63849a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 41 additions and 69 deletions

View File

@ -40,7 +40,7 @@ void FrameBuffer::swap() {
bool set_brightness(int brightness) {
char bright[64];
snprintf(bright, sizeof(bright), "%d", brightness);
return 0 == write_file("/sys/class/leds/lcd-backlight/brightness", bright, strlen(bright));
return 0 == util::write_file("/sys/class/leds/lcd-backlight/brightness", bright, strlen(bright));
}
void FrameBuffer::set_power(int mode) {

View File

@ -15,7 +15,7 @@ int gpio_init(int pin_nr, bool output){
return -1;
}
const char *value = output ? "out" : "in";
return write_file(pin_dir_path, (void*)value, strlen(value));
return util::write_file(pin_dir_path, (void*)value, strlen(value));
}
int gpio_set(int pin_nr, bool high){
@ -25,5 +25,5 @@ int gpio_set(int pin_nr, bool high){
if(pin_val_path_len <= 0){
return -1;
}
return write_file(pin_val_path, (void*)(high ? "1" : "0"), 1);
return util::write_file(pin_val_path, (void*)(high ? "1" : "0"), 1);
}

View File

@ -11,46 +11,6 @@
#include <sched.h>
#endif // __linux__
void* read_file(const char* path, size_t* out_len) {
FILE* f = fopen(path, "r");
if (!f) {
return NULL;
}
fseek(f, 0, SEEK_END);
long f_len = ftell(f);
rewind(f);
// malloc one extra byte so the file will always be NULL terminated
// cl_cached_program_from_file relies on this
char* buf = (char*)malloc(f_len+1);
assert(buf);
size_t num_read = fread(buf, f_len, 1, f);
fclose(f);
if (num_read != 1) {
free(buf);
return NULL;
}
buf[f_len] = '\0';
if (out_len) {
*out_len = f_len;
}
return buf;
}
int write_file(const char* path, const void* data, size_t size, int flags, mode_t mode) {
int fd = open(path, flags, mode);
if (fd == -1) {
return -1;
}
ssize_t n = write(fd, data, size);
close(fd);
return (n >= 0 && (size_t)n == size) ? 0 : -1;
}
void set_thread_name(const char* name) {
#ifdef __linux__
// pthread_setname_np is dumb (fails instead of truncates)
@ -84,3 +44,30 @@ int set_core_affinity(int core) {
return -1;
#endif
}
namespace util {
std::string read_file(const std::string& fn) {
std::ifstream ifs(fn, std::ios::binary | std::ios::ate);
if (ifs) {
std::ifstream::pos_type pos = ifs.tellg();
std::string result;
result.resize(pos);
ifs.seekg(0, std::ios::beg);
ifs.read(result.data(), pos);
if (ifs) return result;
}
return "";
}
int write_file(const char* path, const void* data, size_t size, int flags, mode_t mode) {
int fd = open(path, flags, mode);
if (fd == -1) {
return -1;
}
ssize_t n = write(fd, data, size);
close(fd);
return (n >= 0 && (size_t)n == size) ? 0 : -1;
}
} // namespace util

View File

@ -10,7 +10,6 @@
#include <string>
#include <memory>
#include <atomic>
#include <sstream>
#include <fstream>
#include <thread>
#include <chrono>
@ -20,13 +19,6 @@
typedef void (*sighandler_t)(int sig);
#endif
// Reads a file into a newly allocated buffer.
//
// Returns NULL on failure, otherwise the NULL-terminated file contents.
// The result must be freed by the caller.
void* read_file(const char* path, size_t* out_len);
int write_file(const char* path, const void* data, size_t size, int flags=O_WRONLY, mode_t mode=0777);
void set_thread_name(const char* name);
int set_realtime_priority(int level);
@ -59,12 +51,9 @@ inline std::string string_format(const std::string& format, Args... args) {
return std::string(buf.get(), buf.get() + size - 1);
}
inline std::string read_file(const std::string &fn) {
std::ifstream t(fn);
std::stringstream buffer;
buffer << t.rdbuf();
return buffer.str();
}
std::string read_file(const std::string &fn);
int write_file(const char* path, const void* data, size_t size, int flags = O_WRONLY, mode_t mode = 0777);
inline std::string tohex(const uint8_t* buf, size_t buf_size) {
std::unique_ptr<char[]> hexbuf(new char[buf_size*2+1]);

View File

@ -12,6 +12,6 @@ bool watchdog_kick(){
std::string fn = watchdog_fn_prefix + std::to_string(getpid());
std::string cur_t = std::to_string(nanos_since_boot());
int r = write_file(fn.c_str(), cur_t.data(), cur_t.length(), O_WRONLY | O_CREAT);
int r = util::write_file(fn.c_str(), cur_t.data(), cur_t.length(), O_WRONLY | O_CREAT);
return r == 0;
}

View File

@ -24,14 +24,13 @@ SNPEModel::SNPEModel(const char *path, float *loutput, size_t loutput_size, int
}
assert(zdl::SNPE::SNPEFactory::isRuntimeAvailable(Runtime));
#endif
size_t model_size;
model_data = (uint8_t *)read_file(path, &model_size);
assert(model_data);
model_data = util::read_file(path);
assert(model_data.size() > 0);
// load model
std::unique_ptr<zdl::DlContainer::IDlContainer> container = zdl::DlContainer::IDlContainer::open(model_data, model_size);
std::unique_ptr<zdl::DlContainer::IDlContainer> container = zdl::DlContainer::IDlContainer::open((uint8_t*)model_data.data(), model_data.size());
if (!container) { PrintErrorStringAndExit(); }
printf("loaded model with size: %lu\n", model_size);
printf("loaded model with size: %lu\n", model_data.size());
// create model runner
zdl::SNPE::SNPEBuilder snpeBuilder(container.get());

View File

@ -24,9 +24,6 @@
class SNPEModel : public RunModel {
public:
SNPEModel(const char *path, float *loutput, size_t loutput_size, int runtime);
~SNPEModel() {
if (model_data) free(model_data);
}
void addRecurrent(float *state, int state_size);
void addTrafficConvention(float *state, int state_size);
void addDesire(float *state, int state_size);
@ -37,7 +34,7 @@ public:
#endif
private:
uint8_t *model_data = NULL;
std::string model_data;
#if defined(QCOM) || defined(QCOM2)
zdl::DlSystem::Runtime_t Runtime;

View File

@ -1,2 +1,2 @@
Import('env', 'cereal', 'messaging')
env.Program('proclogd.cc', LIBS=[cereal, messaging, 'pthread', 'zmq', 'capnp', 'kj'])
Import('env', 'cereal', 'messaging', 'common')
env.Program('proclogd.cc', LIBS=[cereal, messaging, 'pthread', 'zmq', 'capnp', 'kj', 'common'])