From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 06ECE4C482 for ; Wed, 12 Feb 2025 09:30:16 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C897C68BF17; Wed, 12 Feb 2025 11:30:02 +0200 (EET) Received: from mail-lf1-f52.google.com (mail-lf1-f52.google.com [209.85.167.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 83D9A68B63D for ; Wed, 12 Feb 2025 11:29:55 +0200 (EET) Received: by mail-lf1-f52.google.com with SMTP id 2adb3069b0e04-54505191cdcso4224458e87.2 for ; Wed, 12 Feb 2025 01:29:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=martin-st.20230601.gappssmtp.com; s=20230601; t=1739352595; x=1739957395; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cl118cCJiZph/BZ9MBfcWTb37bho1DT/r25dm8EypO0=; b=Cw9iiUeJ5Z3MzgAli/mLppUZ6WQ8RxFqzWi8ICrrM0SUZD3dT8ZdhbcY120Goc2Zol Sbb02XY2cB+MiTs1bFWoj/6Mx3jqBZnr5k+jVzFj2jqbwaShrH6BqNRUryBPTWIUCYcz 5UGNtGPvhx2XsjVHvC87gOrEUWBrdaMBVBEgZsasQ0AkmTRBbwyEnax+a8S0f1nsGv5s oUKikZ3ib7GzK0rjiNcbCudELfn1MOqQ3Xy94xgfmW0Ok44TuvV1MZj1dgyULZNRJIgA aLRtULf6O4Z/hpAW79tdJSscQ3oUgM6T/+BZGDQaDDLE37eKz2FurbiyqI5pWLIdZvD4 GJTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739352595; x=1739957395; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cl118cCJiZph/BZ9MBfcWTb37bho1DT/r25dm8EypO0=; b=UHOglVNkzVeBHzZuIyL+MAsJ46urS9Iof4R/em4/ZgkiMr5FdgU+BmnNY4zVTIO87K iUmeCYLCXZWZBdzcqi675fqXEVKcIEa0LQ2Hkm4yUcQ4GdrofJ7l8WbRGKidmc9SvP43 w/x4lu+4LkZqNVHp5dtvGfhgI12Xsq47N8Uls676kc9lweRmjBA0QmYoSZLWTAE4fxrl r+O+r4sqUJRViwE9FmGT1YEnOd06+H/ieE/UU8tQZxuxzx7nKUTwFH9QMOlBSz6yqkCM RR4qKIzviGVmNq5pT0t+qOE9ec6bmUoBKpmSq2rlQDPCN1aeo+ooN+AL+vYVBu2vqHqh a6YQ== X-Gm-Message-State: AOJu0Yzxc138MXWSjMU/cJsDXEQB8092/wPF0WQj5Qc1naFnnOlFpoxE KwUSpRm9sa8dcQzQ8jzhVr5vZyjVZ+EhTHH0sEkZMomrTZn98sNTOTeevYdB8aZ7g5zajD8IqIw +iQ== X-Gm-Gg: ASbGnctftCwdBNhF+xbHpd8Vk6pF1KaMNQ3b6qZWUeq5QqG5vaXaR7BN4FhsAH8T40K P500iNRZFPSPn3kSUiqwy73/0FxvkPc2+coSkhYOpCyl5QBqaRxe7xfNQLtRZz/qSyCJvu5NxFr nfOtnTt1fRY7nqP/TFy8S+Se7YX3Ie42oCQkppHPo/m7XwmbARlcTo7CMwClECmQQNij8iplVvR goBQo+pEw5L5hYRFTWBb96db2puBm/7Zrh7uB4IhqiFgpbqEqi3p4mLgtwvki000lN9tpYb/oTR A2sQFS7aMKuI+Szk3sxn/L28/uOSURgxxNKcmf+lYBO1RS8= X-Google-Smtp-Source: AGHT+IGT+WSww+dhxeaDT9RkjIYhkd/QhOvugSL+14WEI5+IhkaRPKPomjnJnyCvUpbRO43lkO3Rqw== X-Received: by 2002:a05:6512:3d0e:b0:545:93b:30c3 with SMTP id 2adb3069b0e04-545180ef64amr707737e87.18.1739352594548; Wed, 12 Feb 2025 01:29:54 -0800 (PST) Received: from localhost (dsl-tkubng21-50de30-198.dhcp.inet.fi. [80.222.48.198]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-54506158351sm1224028e87.229.2025.02.12.01.29.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2025 01:29:54 -0800 (PST) From: =?UTF-8?q?Martin=20Storsj=C3=B6?= To: ffmpeg-devel@ffmpeg.org Date: Wed, 12 Feb 2025 11:29:49 +0200 Message-ID: <20250212092953.2760237-2-martin@martin.st> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250212092953.2760237-1-martin@martin.st> References: <20250212092953.2760237-1-martin@martin.st> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4 2/2] random_seed: Improve behaviour with small timer increments with high precision timers X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Michael Niedermayer Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: On a Zen 5, on Ubuntu 24.04 (with CLOCKS_PER_SEC 1000000), the value of clock() in this loop increments by 0 most of the time, and when it does increment, it usually increments by 1 compared to the previous round. Due to the "last_t + 2*last_td + (CLOCKS_PER_SEC > 1000) >= t" expression, we only manage to take one step forward in this loop (incrementing i) if clock() increments by 2, while it incremented by 0 in the previous iteration (last_td). This is similar to the change done in c4152fc42e480c41efb7f761b1bbe5f0bc43d5bc, to speed it up on systems with very small CLOCKS_PER_SEC. However in this case, CLOCKS_PER_SEC is still very large, but the machine is fast enough to hit every clock increment repeatedly. For this case, use the number of repetitions of each timer value as entropy source; require a change in the number of repetitions in order to proceed to the next buffer index. This helps the fate-random-seed test to actually terminate within a reasonable time on such a system (where it previously could hang, running for many minutes). --- v4: Keep a history of 2 old unique repeat counts, and require the new count of repeats to differ from the previous 2 unique values in order to count it as new entropy. --- libavutil/random_seed.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/libavutil/random_seed.c b/libavutil/random_seed.c index ca084b40da..e08ab7b12e 100644 --- a/libavutil/random_seed.c +++ b/libavutil/random_seed.c @@ -83,6 +83,7 @@ static uint32_t get_generic_seed(void) static uint32_t buffer[512] = { 0 }; unsigned char digest[20]; uint64_t last_i = i; + int repeats[3] = { 0 }; av_assert0(sizeof(tmp) >= av_sha_size); @@ -101,8 +102,23 @@ static uint32_t get_generic_seed(void) int incremented_i = 0; int cur_td = t - last_t; if (last_t + 2*last_td + (CLOCKS_PER_SEC > 1000) < t) { + // If the timer incremented by more than 2*last_td at once, + // we may e.g. have had a context switch. If the timer resolution + // is high (CLOCKS_PER_SEC > 1000), require that the timer + // incremented by more than 1. If the timer resolution is low, + // it is enough that the timer incremented at all. buffer[++i & 511] += cur_td % 3294638521U; incremented_i = 1; + } else if (t != last_t && repeats[0] > 0 && repeats[1] > 0 && + repeats[2] > 0 && repeats[0] != repeats[1] && + repeats[0] != repeats[2]) { + // If the timer resolution is high, and we get the same timer + // value multiple times, use variances in the number of repeats + // of each timer value as entropy. If we get a different number of + // repeats than the last two unique cases, count that as entropy + // and proceed to the next index. + buffer[++i & 511] += (repeats[0] + repeats[1] + repeats[2]) % 3294638521U; + incremented_i = 1; } else { buffer[i & 511] = 1664525*buffer[i & 511] + 1013904223 + (cur_td % 3294638521U); } @@ -110,6 +126,16 @@ static uint32_t get_generic_seed(void) if (last_i && i - last_i > 4 || i - last_i > 64 || TEST && i - last_i > 8) break; } + if (t == last_t) { + repeats[0]++; + } else { + // If we got a new unique number of repeats, update the history. + if (repeats[0] != repeats[1]) { + repeats[2] = repeats[1]; + repeats[1] = repeats[0]; + } + repeats[0] = 0; + } last_t = t; last_td = cur_td; if (!init_t) -- 2.43.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".