46#define SPEEDTEST_FILENAME_SIZE 512
47#define SPEEDTEST_PROGRESS_CAPACITY ((CAMERA_SPEEDTEST_SAMPLE_COUNT + 1) * 2)
87 int image_number,
char *path,
size_t path_size)
92 sizeof(resolved_directory));
93 int length = snprintf(path, path_size,
"%s/%c%06d.jpg", directory,
95 return length >= 0 && (size_t)length < path_size ? 0 : -1;
109 if (time_ns == NULL || clock_gettime(CLOCK_MONOTONIC, &now) != 0
110 || now.tv_sec < 0 || (uint64_t)now.tv_sec > UINT64_MAX / 1000000000ULL) {
113 *time_ns = (uint64_t)now.tv_sec * 1000000000ULL + (uint64_t)now.tv_nsec;
118 char *
output,
size_t output_size)
120 if (base_directory == NULL ||
output == NULL || output_size == 0) {
126 sizeof(resolved_directory));
131 for (
unsigned attempt = 0; attempt < 100; ++attempt) {
132 int length = snprintf(
output, output_size,
"%s/speedtest-%ld-%" PRIu64
"-%u",
133 base, (
long)getpid(), now_ns, attempt);
134 if (length < 0 || (
size_t)length >= output_size) {
135 errno = ENAMETOOLONG;
138 if (mkdir(
output, 0700) == 0)
return 0;
139 if (errno != EEXIST)
return -1;
156 if (actual_filename == NULL || strcmp(actual_filename, expected_filename) != 0) {
157 fprintf(stderr,
"USHER SPEEDTEST:\tbackend returned output path '%s', expected '%s'\n",
158 actual_filename == NULL ?
"(null)" : actual_filename, expected_filename);
161 struct stat image_status;
162 if (stat(actual_filename, &image_status) != 0) {
163 fprintf(stderr,
"USHER SPEEDTEST:\tunable to validate image %s: %s\n",
164 actual_filename, strerror(errno));
167 if (!S_ISREG(image_status.st_mode) || image_status.st_size <= 0) {
168 fprintf(stderr,
"USHER SPEEDTEST:\tinvalid or empty image %s\n", actual_filename);
190 ? snprintf(line,
sizeof(line),
"USHER SPEEDTEST:\twarm-up capture started: %s\n",
192 : snprintf(line,
sizeof(line),
"USHER SPEEDTEST:\tcapture %u/%u started: %s\n",
196 ? snprintf(line,
sizeof(line),
"USHER SPEEDTEST:\twarm-up capture %s\n",
197 record->succeeded ?
"completed" :
"failed")
198 : snprintf(line,
sizeof(line),
"USHER SPEEDTEST:\tcapture %u/%u %s in %.3f ms\n",
200 record->succeeded ?
"completed" :
"failed",
201 (
double)
record->duration_ns / 1000000.0);
203 if (length < 0 || (
size_t)length >=
sizeof(line))
return -1;
213 written = write(reporter->
output_fd, line, (
size_t)length);
214 }
while (written < 0 && errno == EINTR);
215 return written == length ? 0 : -1;
230 pthread_mutex_lock(&reporter->
mutex);
235 pthread_mutex_unlock(&reporter->
mutex);
241 pthread_mutex_unlock(&reporter->
mutex);
244 pthread_mutex_lock(&reporter->
mutex);
246 pthread_mutex_unlock(&reporter->
mutex);
267 if (fflush(stdout) != 0) {
268 fprintf(stderr,
"USHER SPEEDTEST:\tunable to flush progress output: %s\n",
272 struct stat output_status;
273 if (fstat(STDOUT_FILENO, &output_status) != 0) {
274 fprintf(stderr,
"USHER SPEEDTEST:\tunable to inspect progress output: %s\n",
282 && fcntl(STDOUT_FILENO, F_SETFL, reporter->
stdout_flags | O_APPEND) != 0)) {
283 fprintf(stderr,
"USHER SPEEDTEST:\tunable to configure progress output: %s\n",
288 O_WRONLY | O_NONBLOCK | O_APPEND | O_CLOEXEC);
290 fprintf(stderr,
"USHER SPEEDTEST:\tunable to open bounded progress output: %s\n",
297 int result = pthread_mutex_init(&reporter->
mutex, NULL);
299 fprintf(stderr,
"USHER SPEEDTEST:\tunable to initialize progress mutex: %s\n",
307 fprintf(stderr,
"USHER SPEEDTEST:\tunable to initialize progress condition: %s\n",
309 pthread_mutex_destroy(&reporter->
mutex);
316 fprintf(stderr,
"USHER SPEEDTEST:\tunable to start progress reporter: %s\n",
318 pthread_cond_destroy(&reporter->
available);
319 pthread_mutex_destroy(&reporter->
mutex);
341 unsigned sample,
bool warmup,
bool succeeded,
342 uint64_t duration_ns,
const char *filename)
344 pthread_mutex_lock(&reporter->
mutex);
346 pthread_mutex_unlock(&reporter->
mutex);
347 fprintf(stderr,
"USHER SPEEDTEST:\tprogress queue exhausted\n");
358 int length = snprintf(
record->filename,
sizeof(
record->filename),
"%s",
359 filename == NULL ?
"" : filename);
360 if (length < 0 || (
size_t)length >=
sizeof(
record->filename)) {
361 pthread_mutex_unlock(&reporter->
mutex);
366 pthread_cond_signal(&reporter->
available);
367 pthread_mutex_unlock(&reporter->
mutex);
381 pthread_mutex_lock(&reporter->
mutex);
383 pthread_cond_signal(&reporter->
available);
384 pthread_mutex_unlock(&reporter->
mutex);
385 int join_result = pthread_join(reporter->
thread, NULL);
389 if (lseek(STDOUT_FILENO, 0, SEEK_END) < 0
390 || fcntl(STDOUT_FILENO, F_SETFL, reporter->
stdout_flags) != 0) {
391 output_failed =
true;
394 pthread_cond_destroy(&reporter->
available);
395 pthread_mutex_destroy(&reporter->
mutex);
396 if (join_result != 0) {
397 fprintf(stderr,
"USHER SPEEDTEST:\tunable to stop progress reporter: %s\n",
398 strerror(join_result));
401 return output_failed ? -1 : 0;
407 if (config == NULL || config->
photo_directory == NULL || first_image == NULL
418 errno = ENAMETOOLONG;
422 if (lstat(path, &status) == 0) {
426 if (errno != ENOENT) {
429 if (++consecutive == block_size) {
430 *first_image = image_number - block_size + 1;
451 uint64_t total_ns = 0;
452 uint64_t minimum_ns = UINT64_MAX;
453 uint64_t maximum_ns = 0;
454 uint64_t sequence_start_ns = 0;
455 uint64_t sequence_end_ns = 0;
459 const bool warmup =
capture == 0;
460 const unsigned sample = warmup ? 0 :
capture;
461 const int image_number = first_image + (int)
capture;
466 sizeof(expected_filename)) != 0
468 expected_filename) != 0) {
474 uint64_t completed_ns;
476 fprintf(stderr,
"USHER SPEEDTEST:\tmonotonic clock unavailable: %s\n",
481 if (!warmup && sequence_start_ns == 0) sequence_start_ns = started_ns;
482 int shoot_result = config->
shoot(actual_filename,
sizeof(actual_filename), image_number);
483 if (shoot_result == 0 && (!actual_filename[0]
484 || strnlen(actual_filename,
sizeof(actual_filename)) ==
sizeof(actual_filename))) {
489 fprintf(stderr,
"USHER SPEEDTEST:\tinvalid monotonic capture interval\n");
493 uint64_t duration_ns = completed_ns - started_ns;
496 if (publication < 0) shoot_result = -1;
497 else if (publication > 0)
498 fprintf(stderr,
"USHER SPEEDTEST:\tphoto published with durability/cleanup warning: %s\n", strerror(errno));
500 bool succeeded = shoot_result == 0
503 duration_ns, expected_filename) != 0) {
508 fprintf(stderr,
"USHER SPEEDTEST:\t%s capture failed; aborting benchmark\n",
509 warmup ?
"warm-up" :
"measured");
514 fprintf(stderr,
"USHER SPEEDTEST:\tinterrupted after completed capture; aborting benchmark\n");
518 if (warmup)
continue;
520 sequence_end_ns = completed_ns;
521 if (UINT64_MAX - total_ns < duration_ns) {
522 fprintf(stderr,
"USHER SPEEDTEST:\tcapture duration overflow\n");
526 total_ns += duration_ns;
527 if (duration_ns < minimum_ns) minimum_ns = duration_ns;
528 if (duration_ns > maximum_ns) maximum_ns = duration_ns;
532 if (
result != 0)
return -1;
533 if (total_ns == 0 || minimum_ns == 0 || sequence_end_ns <= sequence_start_ns) {
534 fprintf(stderr,
"USHER SPEEDTEST:\tinvalid zero-duration benchmark\n");
538 const double total_seconds = (double)total_ns / 1000000000.0;
539 const double sequence_seconds = (double)(sequence_end_ns - sequence_start_ns) / 1000000000.0;
542 printf(
"USHER SPEEDTEST:\tbackend: %s\n", config->
backend_name);
543 printf(
"USHER SPEEDTEST:\tmeasured captures: %u (one warm-up excluded)\n",
545 printf(
"USHER SPEEDTEST:\tpractical sustained speed: %.3f photos/s "
546 "(1 photo every %.3f s, %.1f ms)\n",
547 1.0 / sustained_seconds_per_photo, sustained_seconds_per_photo,
548 sustained_seconds_per_photo * 1000.0);
549 printf(
"USHER SPEEDTEST:\taverage camera-call speed: %.3f photos/s "
550 "(1 photo every %.3f s, %.1f ms)\n",
551 1.0 / capture_seconds_per_photo, capture_seconds_per_photo,
552 capture_seconds_per_photo * 1000.0);
553 printf(
"USHER SPEEDTEST:\taverage / fastest / slowest capture time: "
554 "%.3f / %.3f / %.3f ms\n",
555 capture_seconds_per_photo * 1000.0,
556 (
double)minimum_ns / 1000000.0, (
double)maximum_ns / 1000000.0);
557 printf(
"USHER SPEEDTEST:\tfastest single capture: %.3f ms "
558 "(equivalent to %.3f photos/s)\n",
559 (
double)minimum_ns / 1000000.0, 1000000000.0 / (
double)minimum_ns);
560 printf(
"USHER SPEEDTEST:\ttotal camera-call time for %u photos: %.6f s\n",
562 printf(
"USHER SPEEDTEST:\timages retained in %s from %c%06d.jpg through %c%06d.jpg\n",
Optional split-phase capture contract for USHER image backends.
static bool capture_running(const volatile sig_atomic_t *running)
Atomically poll a borrowed cancellation flag; NULL means no cancellation.
static int write_progress(struct progress_reporter *reporter, const struct progress_record *record)
Write one progress record without waiting indefinitely for stdout.
static int format_image_path(const struct camera_speedtest_config *config, int image_number, char *path, size_t path_size)
Derive the expected JPEG path for a benchmark capture.
static int reporter_enqueue(struct progress_reporter *reporter, enum progress_kind kind, unsigned sample, bool warmup, bool succeeded, uint64_t duration_ns, const char *filename)
Queue one capture lifecycle event without performing output I/O.
int camera_speedtest_create_output_directory(const char *base_directory, char *output, size_t output_size)
Atomically create a private directory for one benchmark run.
static void * report_progress(void *argument)
Drain queued progress records on the dedicated reporter thread.
int camera_speedtest_run(const struct camera_speedtest_config *config, int first_image)
Capture one untimed warm-up image and ten timed images sequentially.
static int monotonic_time_ns(uint64_t *time_ns)
Read a non-negative monotonic timestamp in nanoseconds.
static int reporter_start(struct progress_reporter *reporter)
Initialize and launch the bounded progress reporter.
static int validate_capture(const char *actual_filename, const char *expected_filename)
Confirm that a backend produced exactly the requested usable image.
#define SPEEDTEST_FILENAME_SIZE
#define SPEEDTEST_PROGRESS_CAPACITY
int camera_speedtest_find_image_block(const struct camera_speedtest_config *config, int *first_image)
Find an unused consecutive image-number block for warm-up and samples.
static int reporter_stop(struct progress_reporter *reporter)
Request reporter shutdown, drain queued output, and release its resources.
Standalone, backend-neutral still-camera throughput benchmark.
#define CAMERA_SPEEDTEST_SAMPLE_COUNT
#define CAMERA_SPEEDTEST_FIRST_IMAGE
#define CAMERA_SPEEDTEST_LAST_IMAGE
void(* close)(struct usbfs_handle *)
static bool capture(struct transport *transport, FILE *file, const struct capture_settings *settings)
Capture to the camera's SD card and download the resulting JPEG.
int(* open)(const struct usbfs_device *, struct usbfs_handle *)
static bool poll_result(struct camera_point_result *point)
Drain a bounded reply batch and publish only the active request's result.
static int record(const union dc_shot_union *shot)
Retrieve a point and admit a new observation only when its credit is free.
Small path and writable-directory helpers shared by USHER backends.
static int usher_complete_photo(char *filename, size_t capacity)
Publish a staged photo and return its immutable public path.
static const char * usher_resolve_path(const char *path, char *buffer, size_t buffer_size)
Expand a leading home-directory shorthand without allocating memory.
static int usher_ensure_directory(const char *path)
Create missing directory components and verify the final directory is writable.
Immutable dependencies for one selected and initialized camera backend.
const char * photo_directory
volatile sig_atomic_t * keep_running
const char * backend_name
int(* shoot)(char *filename, size_t filename_size, int image_number)
char filename[SPEEDTEST_FILENAME_SIZE]
struct progress_record records[SPEEDTEST_PROGRESS_CAPACITY]
struct usher_control_result result