Use monotonic clock for cryptfs progress

Otherwise we get strange results when the time changes. Worst
effect is that the encryption takes a lot longer since we are
calling the logging code far more frequently.

Bug: 17625981
Change-Id: Ice29f28b3720e9e4a1ea28e45eeab574d1959ec1
This commit is contained in:
Paul Lawrence 2014-09-30 09:12:51 -07:00
parent 3574b085f4
commit 9c58a871f9

View file

@ -40,6 +40,7 @@
#include <ext4.h> #include <ext4.h>
#include <linux/kdev_t.h> #include <linux/kdev_t.h>
#include <fs_mgr.h> #include <fs_mgr.h>
#include <time.h>
#include "cryptfs.h" #include "cryptfs.h"
#define LOG_TAG "Cryptfs" #define LOG_TAG "Cryptfs"
#include "cutils/log.h" #include "cutils/log.h"
@ -2127,21 +2128,26 @@ static void update_progress(struct encryptGroupsData* data, int is_used)
} }
if (data->cur_pct >= 5) { if (data->cur_pct >= 5) {
double elapsed_time = difftime(time(NULL), data->time_started); struct timespec time_now;
off64_t remaining_blocks = data->tot_used_blocks if (clock_gettime(CLOCK_MONOTONIC, &time_now)) {
- data->used_blocks_already_done; SLOGW("Error getting time");
int remaining_time = (int)(elapsed_time * remaining_blocks } else {
/ data->used_blocks_already_done); double elapsed_time = difftime(time_now.tv_sec, data->time_started);
off64_t remaining_blocks = data->tot_used_blocks
- data->used_blocks_already_done;
int remaining_time = (int)(elapsed_time * remaining_blocks
/ data->used_blocks_already_done);
// Change time only if not yet set, lower, or a lot higher for // Change time only if not yet set, lower, or a lot higher for
// best user experience // best user experience
if (data->remaining_time == -1 if (data->remaining_time == -1
|| remaining_time < data->remaining_time || remaining_time < data->remaining_time
|| remaining_time > data->remaining_time + 60) { || remaining_time > data->remaining_time + 60) {
char buf[8]; char buf[8];
snprintf(buf, sizeof(buf), "%d", remaining_time); snprintf(buf, sizeof(buf), "%d", remaining_time);
property_set("vold.encrypt_time_remaining", buf); property_set("vold.encrypt_time_remaining", buf);
data->remaining_time = remaining_time; data->remaining_time = remaining_time;
}
} }
} }
} }
@ -2349,7 +2355,13 @@ static int cryptfs_enable_inplace_ext4(char *crypto_blkdev,
data.one_pct = data.tot_used_blocks / 100; data.one_pct = data.tot_used_blocks / 100;
data.cur_pct = 0; data.cur_pct = 0;
data.time_started = time(NULL);
struct timespec time_started = {0};
if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
SLOGW("Error getting time at start");
// Note - continue anyway - we'll run with 0
}
data.time_started = time_started.tv_sec;
data.remaining_time = -1; data.remaining_time = -1;
rc = encrypt_groups(&data); rc = encrypt_groups(&data);