common : throttle download progress output to reduce IO flush (#17427)
This change limits progress updates to approximately every 0.1% of the file size to minimize stdio overhead. Also fixes compiler warnings regarding __func__ in lambdas. Signed-off-by: Adrien Gallouët <angt@huggingface.co>
This commit is contained in:
parent
def5404f26
commit
beb1f0c503
|
|
@ -517,16 +517,18 @@ static bool common_pull_file(httplib::Client & cli,
|
||||||
headers.emplace("Range", "bytes=" + std::to_string(existing_size) + "-");
|
headers.emplace("Range", "bytes=" + std::to_string(existing_size) + "-");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::atomic<size_t> downloaded{existing_size};
|
const char * func = __func__; // avoid __func__ inside a lambda
|
||||||
|
size_t downloaded = existing_size;
|
||||||
|
size_t progress_step = 0;
|
||||||
|
|
||||||
auto res = cli.Get(resolve_path, headers,
|
auto res = cli.Get(resolve_path, headers,
|
||||||
[&](const httplib::Response &response) {
|
[&](const httplib::Response &response) {
|
||||||
if (existing_size > 0 && response.status != 206) {
|
if (existing_size > 0 && response.status != 206) {
|
||||||
LOG_WRN("%s: server did not respond with 206 Partial Content for a resume request. Status: %d\n", __func__, response.status);
|
LOG_WRN("%s: server did not respond with 206 Partial Content for a resume request. Status: %d\n", func, response.status);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (existing_size == 0 && response.status != 200) {
|
if (existing_size == 0 && response.status != 200) {
|
||||||
LOG_WRN("%s: download received non-successful status code: %d\n", __func__, response.status);
|
LOG_WRN("%s: download received non-successful status code: %d\n", func, response.status);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (total_size == 0 && response.has_header("Content-Length")) {
|
if (total_size == 0 && response.has_header("Content-Length")) {
|
||||||
|
|
@ -534,7 +536,7 @@ static bool common_pull_file(httplib::Client & cli,
|
||||||
size_t content_length = std::stoull(response.get_header_value("Content-Length"));
|
size_t content_length = std::stoull(response.get_header_value("Content-Length"));
|
||||||
total_size = existing_size + content_length;
|
total_size = existing_size + content_length;
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
LOG_WRN("%s: invalid Content-Length header: %s\n", __func__, e.what());
|
LOG_WRN("%s: invalid Content-Length header: %s\n", func, e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -542,11 +544,16 @@ static bool common_pull_file(httplib::Client & cli,
|
||||||
[&](const char *data, size_t len) {
|
[&](const char *data, size_t len) {
|
||||||
ofs.write(data, len);
|
ofs.write(data, len);
|
||||||
if (!ofs) {
|
if (!ofs) {
|
||||||
LOG_ERR("%s: error writing to file: %s\n", __func__, path_tmp.c_str());
|
LOG_ERR("%s: error writing to file: %s\n", func, path_tmp.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
downloaded += len;
|
downloaded += len;
|
||||||
|
progress_step += len;
|
||||||
|
|
||||||
|
if (progress_step >= total_size / 1000 || downloaded == total_size) {
|
||||||
print_progress(downloaded, total_size);
|
print_progress(downloaded, total_size);
|
||||||
|
progress_step = 0;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
nullptr
|
nullptr
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue