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 158914C16C for ; Wed, 5 Feb 2025 22:18:35 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1BCE368B9E7; Thu, 6 Feb 2025 00:18:25 +0200 (EET) Received: from mail-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7121F68B528 for ; Thu, 6 Feb 2025 00:18:16 +0200 (EET) Received: by mail-lj1-f182.google.com with SMTP id 38308e7fff4ca-30615661f98so2958111fa.2 for ; Wed, 05 Feb 2025 14:18:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=martin-st.20230601.gappssmtp.com; s=20230601; t=1738793895; x=1739398695; 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=GriUGRTiYblsL9eYhrL0r9YZXl6ccSn95hhfEnsIaw8=; b=MI4N6sSMjOdr0vWXZ0OiY3cbUdDn4CPNsRm5s5PVFbxbpVSn25vbA18xyL5qc0uFQq tYXC4ngG2dml7wGxSi1Uxz8PzQMu7DlOiMfbnBYsC+VoR3z8c2Rv72C3DEh3PhfEDa3p pti/h4iRXfM0g/A5iq+FUv7lm4vmGnTMoaMIyKDfJey0ENS0ERsuiXvFNYOeYuPnO7i8 khKezIH77ogOWMlXyin3+zbQhaeeyx+Zkg/o1mQB2NUiFS9s4CvkoECet+BlYwXMNdti 9YRpw8tWC2Ewu/L2DQn/DaOxiQD0M+1xjRBrBpE70WOhdfo0PyuRfpn3bgZhGJM7E+sA 3OTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738793895; x=1739398695; 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=GriUGRTiYblsL9eYhrL0r9YZXl6ccSn95hhfEnsIaw8=; b=GOh59IqiyIl6P1tcmyAZ/wQH3aMykZGAmN7zC+pqGY/vYvYZGH17j+DV/2hdbcOLzY U6ugKTS2reAnrMYaNcNF0tmMbkNkbPKyVUqLoDGI4vrPpi1eKnAxCBqh2gp+2E6v+xxP 0mB8sszpVHbOPL7JEuu0DEFhu1TWz8ha3KCHUkAS9xFo71ay1hH4xkxbyt90IWX2QWpe /DaW2Bw9/IjgbPzZwsbeuHDqOtOFydqsi/4MCxKDch9leEY4aGATKpnlu4y7VGk3tWzp kEnf4o2uJkdC3FWYDdt90znQA/RDshYw91TZcXb3CztgaffGF+CN6sdQ7+VhIXFWfsO/ 0elg== X-Gm-Message-State: AOJu0YyECNFdj4iP+rb8SOgBzq2tl3k2sDrM/X2tIaOPXfGT4QadGyvD HsSF+qKZn71RpvPHJV6YTozAVAfQzZVfHI/rK9TvI5Hi3AH1o9850KHB+3Fl4NRGdVBwuZ3pm89 LtQ== X-Gm-Gg: ASbGnctCTu8PoiAp0+B/6v2vnu4fBHDieaZasugMxwB6xMDKRzcKa4RGheQzCFCVRom rs+ZShvG0A73nnKQmZ8Urm2v18mGo/cCdow1R+vmqJz0xZo9u00s9kenx+irsRHFworBUhF71Q+ 3L6N7Wai9Zln+nkqXYEGXTSltM5gA7QQ7jw53AOaGrD1xWD8LM1EHT4FRfyZCd/H0Cz9htwdqW5 7VyQ6Uy8myc0pAkNkUfet587zI2DR26UuBaIq+aOo3ETEV/ud+YyD0LLdItT6bMQ1h6pIT442a6 /3cm4wr5XxBNNwr+SMQbd2YLkQqdOyE3x2//kor1geMLVm0= X-Google-Smtp-Source: AGHT+IELfFSOltu7Y4FA9V49ZyznTJCt2YgYxG13FVI1U08nmqmGoYARiJP6ex9t7J3kpS0baAPhKg== X-Received: by 2002:a05:6512:4005:b0:540:23af:837e with SMTP id 2adb3069b0e04-54405a7a41emr1589751e87.39.1738793895353; Wed, 05 Feb 2025 14:18:15 -0800 (PST) Received: from localhost (dsl-tkubng21-50de30-198.dhcp.inet.fi. [80.222.48.198]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-543ebeb0a7fsm1966713e87.113.2025.02.05.14.18.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Feb 2025 14:18:14 -0800 (PST) From: =?UTF-8?q?Martin=20Storsj=C3=B6?= To: ffmpeg-devel@ffmpeg.org Date: Thu, 6 Feb 2025 00:18:09 +0200 Message-ID: <20250205221813.4110398-2-martin@martin.st> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250205221813.4110398-1-martin@martin.st> References: <20250205221813.4110398-1-martin@martin.st> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 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.cc 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). --- libavutil/random_seed.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/libavutil/random_seed.c b/libavutil/random_seed.c index ca084b40da..adb7b1f717 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 last_repeat = 0, cur_repeat = 0; av_assert0(sizeof(tmp) >= av_sha_size); @@ -101,8 +102,21 @@ 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 && cur_repeat > 0 && last_repeat > 0 && + cur_repeat != last_repeat) { + // 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 the number of repeats changed, + // proceed to the next index. + buffer[++i & 511] += (cur_repeat + last_repeat) % 3294638521U; + incremented_i = 1; } else { buffer[i & 511] = 1664525*buffer[i & 511] + 1013904223 + (cur_td % 3294638521U); } @@ -110,6 +124,12 @@ 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) { + cur_repeat++; + } else { + last_repeat = cur_repeat; + cur_repeat = 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".