feat: prepare M8 release candidate
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package org.neslite.app.library
|
||||
|
||||
import androidx.room.testing.MigrationTestHelper
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class GameDatabaseMigrationTest {
|
||||
@get:Rule
|
||||
val helper = MigrationTestHelper(
|
||||
InstrumentationRegistry.getInstrumentation(),
|
||||
GameDatabase::class.java,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun migration1To2PreservesLibraryAndAddsDailyUseState() {
|
||||
helper.createDatabase(TEST_DATABASE, 1).apply {
|
||||
execSQL(
|
||||
"INSERT INTO games " +
|
||||
"(sha256, displayName, sizeBytes, format, fileName, status, addedAtEpochMs) " +
|
||||
"VALUES ('hash-a', '中文长文件名.nes', 40976, 'iNES', 'hash-a.nes', 'READY', 1234)",
|
||||
)
|
||||
close()
|
||||
}
|
||||
|
||||
helper.runMigrationsAndValidate(TEST_DATABASE, 2, true, GameDatabase.MIGRATION_1_2).use { database ->
|
||||
database.query(
|
||||
"SELECT displayName, favorite, hidden, lastPlayedAtEpochMs FROM games WHERE sha256='hash-a'",
|
||||
).use { cursor ->
|
||||
check(cursor.moveToFirst())
|
||||
assertEquals("中文长文件名.nes", cursor.getString(0))
|
||||
assertEquals(0, cursor.getInt(1))
|
||||
assertEquals(0, cursor.getInt(2))
|
||||
assertEquals(0L, cursor.getLong(3))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TEST_DATABASE = "m8-migration-test"
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ set(NESLITE_COMMON_SOURCES
|
||||
session_rules.cpp
|
||||
audio_ring_buffer.cpp
|
||||
linear_resampler.cpp
|
||||
input_latency_metrics.cpp
|
||||
timing_metrics.cpp
|
||||
video_buffer.cpp
|
||||
sha256.cpp
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "libretro.h"
|
||||
#include "audio_engine.h"
|
||||
#include "linear_resampler.h"
|
||||
#include "input_latency_metrics.h"
|
||||
#include "safe_commit.h"
|
||||
#include "save_package.h"
|
||||
#include "save_policy.h"
|
||||
@@ -101,6 +102,8 @@ struct Host {
|
||||
std::atomic<std::uint16_t> joypad{0};
|
||||
std::atomic<std::uint16_t> turbo{0};
|
||||
std::atomic<std::uint16_t> effective_joypad{0};
|
||||
std::atomic<std::uint64_t> input_event_generation{0};
|
||||
std::atomic<std::int64_t> input_event_received_ns{0};
|
||||
std::atomic<std::uint64_t> issued_session{0};
|
||||
std::atomic<SessionState> state{SessionState::Idle};
|
||||
std::atomic<std::uint64_t> session_id{0};
|
||||
@@ -122,6 +125,8 @@ struct Host {
|
||||
AudioEngine audio{audio_buffer};
|
||||
LinearStereoResampler resampler;
|
||||
TimingMetrics timing;
|
||||
InputLatencyMetrics input_latency;
|
||||
std::uint64_t sampled_input_generation = 0;
|
||||
std::atomic<bool> fast_forward{false};
|
||||
std::atomic<std::size_t> audio_target_frames{800};
|
||||
double turbo_simulated_seconds = 0.0;
|
||||
@@ -503,6 +508,9 @@ struct Host {
|
||||
joypad.store(0, std::memory_order_relaxed);
|
||||
turbo.store(0, std::memory_order_relaxed);
|
||||
effective_joypad.store(0, std::memory_order_relaxed);
|
||||
input_event_received_ns.store(0, std::memory_order_relaxed);
|
||||
sampled_input_generation = input_event_generation.load(std::memory_order_acquire);
|
||||
input_latency.reset();
|
||||
turbo_simulated_seconds = 0.0;
|
||||
previous_turbo_mask = 0;
|
||||
save_schedule.reset();
|
||||
@@ -888,7 +896,15 @@ struct Host {
|
||||
host.audio_buffer.push(converted.data(), converted.size() / 2U);
|
||||
return frames;
|
||||
}
|
||||
static void input_poll_callback() {}
|
||||
static void input_poll_callback() {
|
||||
Host& host = instance();
|
||||
const std::uint64_t generation = host.input_event_generation.load(std::memory_order_acquire);
|
||||
if (generation != host.sampled_input_generation) {
|
||||
host.input_latency.record(host.input_event_received_ns.load(std::memory_order_acquire),
|
||||
monotonic_now_ns());
|
||||
host.sampled_input_generation = generation;
|
||||
}
|
||||
}
|
||||
static std::int16_t input_state_callback(unsigned port, unsigned device, unsigned, unsigned id) {
|
||||
if (port != 0 || device != RETRO_DEVICE_JOYPAD) return 0;
|
||||
const std::uint16_t mask = instance().effective_joypad.load(std::memory_order_relaxed);
|
||||
@@ -938,7 +954,11 @@ bool save_package_command(std::uint64_t session_id, const std::string& command,
|
||||
}
|
||||
|
||||
void set_joypad_mask(std::uint16_t mask) {
|
||||
Host::instance().joypad.store(mask, std::memory_order_relaxed);
|
||||
Host& host = Host::instance();
|
||||
if (host.joypad.exchange(mask, std::memory_order_acq_rel) != mask) {
|
||||
host.input_event_received_ns.store(monotonic_now_ns(), std::memory_order_release);
|
||||
host.input_event_generation.fetch_add(1, std::memory_order_release);
|
||||
}
|
||||
}
|
||||
|
||||
void set_turbo_mask(std::uint16_t mask) {
|
||||
@@ -950,6 +970,7 @@ std::string session_status_json() {
|
||||
std::lock_guard lock(host.mutex);
|
||||
const AudioDiagnostics audio = host.audio.diagnostics();
|
||||
const TimingSnapshot timing = host.timing.snapshot(monotonic_now_ns());
|
||||
const InputLatencySnapshot input_latency = host.input_latency.snapshot();
|
||||
std::ostringstream json;
|
||||
json << "{\"sessionId\":" << host.session_id.load(std::memory_order_acquire)
|
||||
<< ",\"state\":\"" << session_state_name(host.state.load(std::memory_order_acquire))
|
||||
@@ -963,7 +984,13 @@ std::string session_status_json() {
|
||||
<< ",\"recoverySignature\":\"" << json_escape(host.recovery_signature) << "\""
|
||||
<< ",\"input\":{\"baseMask\":" << host.joypad.load(std::memory_order_relaxed)
|
||||
<< ",\"turboMask\":" << host.turbo.load(std::memory_order_relaxed)
|
||||
<< ",\"effectiveMask\":" << host.effective_joypad.load(std::memory_order_relaxed) << "}"
|
||||
<< ",\"effectiveMask\":" << host.effective_joypad.load(std::memory_order_relaxed)
|
||||
<< ",\"latencyClock\":\"native_steady_clock_jni_to_first_core_sample\""
|
||||
<< ",\"latencyCapacity\":2048"
|
||||
<< ",\"latencySamples\":" << input_latency.samples
|
||||
<< ",\"latencyAverageMs\":" << input_latency.average_ms
|
||||
<< ",\"latencyP95Ms\":" << input_latency.p95_ms
|
||||
<< ",\"latencyMaximumMs\":" << input_latency.maximum_ms << "}"
|
||||
<< ",\"audio\":{\"open\":" << (audio.open ? "true" : "false")
|
||||
<< ",\"active\":" << (audio.active ? "true" : "false")
|
||||
<< ",\"muted\":" << (audio.muted ? "true" : "false")
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#include "input_latency_metrics.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <numeric>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
namespace neslite {
|
||||
namespace {
|
||||
constexpr double kNanosPerMillisecond = 1'000'000.0;
|
||||
}
|
||||
|
||||
InputLatencyMetrics::InputLatencyMetrics(std::size_t capacity) : capacity_(capacity) {
|
||||
if (capacity_ == 0) throw std::invalid_argument("input latency capacity must be positive");
|
||||
}
|
||||
|
||||
void InputLatencyMetrics::reset() {
|
||||
std::lock_guard lock(mutex_);
|
||||
samples_ns_.clear();
|
||||
}
|
||||
|
||||
void InputLatencyMetrics::record(std::int64_t received_ns, std::int64_t sampled_ns) {
|
||||
if (received_ns <= 0 || sampled_ns < received_ns) return;
|
||||
std::lock_guard lock(mutex_);
|
||||
if (samples_ns_.size() == capacity_) samples_ns_.pop_front();
|
||||
samples_ns_.push_back(sampled_ns - received_ns);
|
||||
}
|
||||
|
||||
InputLatencySnapshot InputLatencyMetrics::snapshot() const {
|
||||
std::lock_guard lock(mutex_);
|
||||
if (samples_ns_.empty()) return {};
|
||||
std::vector<std::int64_t> sorted(samples_ns_.begin(), samples_ns_.end());
|
||||
std::sort(sorted.begin(), sorted.end());
|
||||
const std::size_t p95_index = static_cast<std::size_t>(
|
||||
std::ceil(static_cast<double>(sorted.size()) * 0.95)) - 1U;
|
||||
const std::int64_t total = std::accumulate(sorted.begin(), sorted.end(), std::int64_t{0});
|
||||
return InputLatencySnapshot{
|
||||
.samples = sorted.size(),
|
||||
.average_ms = static_cast<double>(total) / static_cast<double>(sorted.size()) /
|
||||
kNanosPerMillisecond,
|
||||
.p95_ms = static_cast<double>(sorted[p95_index]) / kNanosPerMillisecond,
|
||||
.maximum_ms = static_cast<double>(sorted.back()) / kNanosPerMillisecond,
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace neslite
|
||||
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <mutex>
|
||||
|
||||
namespace neslite {
|
||||
|
||||
struct InputLatencySnapshot {
|
||||
std::size_t samples = 0;
|
||||
double average_ms = 0.0;
|
||||
double p95_ms = 0.0;
|
||||
double maximum_ms = 0.0;
|
||||
};
|
||||
|
||||
class InputLatencyMetrics {
|
||||
public:
|
||||
explicit InputLatencyMetrics(std::size_t capacity = 2048);
|
||||
void reset();
|
||||
void record(std::int64_t received_ns, std::int64_t sampled_ns);
|
||||
InputLatencySnapshot snapshot() const;
|
||||
|
||||
private:
|
||||
const std::size_t capacity_;
|
||||
mutable std::mutex mutex_;
|
||||
std::deque<std::int64_t> samples_ns_;
|
||||
};
|
||||
|
||||
} // namespace neslite
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "audio_ring_buffer.h"
|
||||
#include "linear_resampler.h"
|
||||
#include "input_latency_metrics.h"
|
||||
#include "native_smoke.h"
|
||||
#include "session_rules.h"
|
||||
#include "timing_metrics.h"
|
||||
@@ -108,6 +109,18 @@ int main() {
|
||||
}
|
||||
if (ntsc_pulses != 15 || pal_pulses != 15 || neslite::turbo_gate(-1.0)) return 40;
|
||||
|
||||
neslite::InputLatencyMetrics input_latency(4);
|
||||
input_latency.record(1'000'000, 2'000'000);
|
||||
input_latency.record(1'000'000, 3'000'000);
|
||||
input_latency.record(1'000'000, 4'000'000);
|
||||
input_latency.record(1'000'000, 5'000'000);
|
||||
input_latency.record(1'000'000, 10'000'000);
|
||||
const auto latency = input_latency.snapshot();
|
||||
if (latency.samples != 4 || std::abs(latency.average_ms - 4.5) > 0.0001 ||
|
||||
std::abs(latency.p95_ms - 9.0) > 0.0001 || std::abs(latency.maximum_ms - 9.0) > 0.0001) return 63;
|
||||
input_latency.reset();
|
||||
if (input_latency.snapshot().samples != 0) return 64;
|
||||
|
||||
const std::vector<std::uint8_t> abc = {'a', 'b', 'c'};
|
||||
if (neslite::sha256_hex(abc) != "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") return 41;
|
||||
|
||||
|
||||
@@ -1645,9 +1645,11 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
}
|
||||
val audio = status.optJSONObject("audio") ?: JSONObject()
|
||||
val timing = status.optJSONObject("timing") ?: JSONObject()
|
||||
val input = status.optJSONObject("input") ?: JSONObject()
|
||||
audioStatus.text = "音频:${audio.optString("mode")},核心/设备 ${audio.optInt("coreRate")}/${audio.optInt("deviceRate")} Hz\n" +
|
||||
"回调 ${audio.optLong("callbacks")},帧/回调 ${audio.optInt("maxCallbackFrames")},缓冲 ${audio.optInt("bufferLevel")}/${audio.optInt("bufferTarget")}/${audio.optInt("bufferCapacity")},欠载 ${audio.optLong("underrunFrames")},重建 ${audio.optLong("rebuilds")}\n" +
|
||||
"速度 ${String.format(Locale.ROOT, "%.3f", timing.optDouble("speedRatio"))}×,模拟帧 ${timing.optLong("frames")},帧工作 ${String.format(Locale.ROOT, "%.2f", timing.optDouble("averageFrameWorkMs"))}/${String.format(Locale.ROOT, "%.2f", timing.optDouble("maximumFrameWorkMs"))} ms"
|
||||
"速度 ${String.format(Locale.ROOT, "%.3f", timing.optDouble("speedRatio"))}×,模拟帧 ${timing.optLong("frames")},帧工作 ${String.format(Locale.ROOT, "%.2f", timing.optDouble("averageFrameWorkMs"))}/${String.format(Locale.ROOT, "%.2f", timing.optDouble("maximumFrameWorkMs"))} ms\n" +
|
||||
"输入延迟 ${input.optLong("latencySamples")}/1000,P95 ${String.format(Locale.ROOT, "%.2f", input.optDouble("latencyP95Ms"))} ms(JNI→核心首次采样)"
|
||||
}.onFailure { emulatorStatus.text = raw }
|
||||
}
|
||||
mainHandler.postDelayed(::pollSessionStatus, 250)
|
||||
|
||||
@@ -8,7 +8,7 @@ import androidx.room.RoomDatabase;
|
||||
import androidx.room.migration.Migration;
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase;
|
||||
|
||||
@Database(entities = {GameEntity.class}, version = 2, exportSchema = false)
|
||||
@Database(entities = {GameEntity.class}, version = 2, exportSchema = true)
|
||||
public abstract class GameDatabase extends RoomDatabase {
|
||||
private static volatile GameDatabase instance;
|
||||
|
||||
@@ -30,7 +30,7 @@ public abstract class GameDatabase extends RoomDatabase {
|
||||
return current;
|
||||
}
|
||||
|
||||
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
|
||||
public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
|
||||
@Override public void migrate(@NonNull SupportSQLiteDatabase database) {
|
||||
database.execSQL("ALTER TABLE games ADD COLUMN favorite INTEGER NOT NULL DEFAULT 0");
|
||||
database.execSQL("ALTER TABLE games ADD COLUMN hidden INTEGER NOT NULL DEFAULT 0");
|
||||
|
||||
@@ -7,7 +7,7 @@ class BuildIdentityTest {
|
||||
@Test
|
||||
fun debugBuildUsesTheDocumentedIdentity() {
|
||||
assertEquals("org.neslite.app.debug", BuildConfig.APPLICATION_ID)
|
||||
assertEquals("0.7.4-debug", BuildConfig.VERSION_NAME)
|
||||
assertEquals(7004, BuildConfig.VERSION_CODE)
|
||||
assertEquals("0.9.4-debug", BuildConfig.VERSION_NAME)
|
||||
assertEquals(9004, BuildConfig.VERSION_CODE)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user