Upload Kmake

This commit is contained in:
Gorochu
2026-05-26 23:36:42 -07:00
parent ba051b2f74
commit 555ec72358
41615 changed files with 13344630 additions and 1 deletions

View File

@ -0,0 +1,161 @@
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests of the circular queue.
#include "src/init/v8.h"
#include "src/profiler/circular-queue-inl.h"
#include "test/unittests/test-utils.h"
#include "testing/gtest/include/gtest/gtest.h"
using i::SamplingCircularQueue;
using CircularQueueTest = ::testing::Test;
TEST_F(CircularQueueTest, SamplingCircularQueue) {
using Record = v8::base::AtomicWord;
const int kMaxRecordsInQueue = 4;
SamplingCircularQueue<Record, kMaxRecordsInQueue> scq;
// Check that we are using non-reserved values.
// Fill up the first chunk.
CHECK(!scq.Peek());
for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) {
Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue());
CHECK(rec);
*rec = i;
scq.FinishEnqueue();
}
// The queue is full, enqueue is not allowed.
CHECK(!scq.StartEnqueue());
// Try to enqueue when the the queue is full. Consumption must be available.
CHECK(scq.Peek());
for (int i = 0; i < 10; ++i) {
Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue());
CHECK(!rec);
CHECK(scq.Peek());
}
// Consume all records.
for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) {
Record* rec = reinterpret_cast<Record*>(scq.Peek());
CHECK(rec);
CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
scq.Remove();
CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
}
// The queue is empty.
CHECK(!scq.Peek());
CHECK(!scq.Peek());
for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) {
Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue());
CHECK(rec);
*rec = i;
scq.FinishEnqueue();
}
// Consume all available kMaxRecordsInQueue / 2 records.
CHECK(scq.Peek());
for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) {
Record* rec = reinterpret_cast<Record*>(scq.Peek());
CHECK(rec);
CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
scq.Remove();
CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
}
// The queue is empty.
CHECK(!scq.Peek());
}
namespace {
using Record = v8::base::AtomicWord;
using TestSampleQueue = SamplingCircularQueue<Record, 12>;
class ProducerThread : public v8::base::Thread {
public:
ProducerThread(TestSampleQueue* scq, int records_per_chunk, Record value,
v8::base::Semaphore* finished)
: Thread(Options("producer")),
scq_(scq),
records_per_chunk_(records_per_chunk),
value_(value),
finished_(finished) {}
void Run() override {
for (Record i = value_; i < value_ + records_per_chunk_; ++i) {
Record* rec = reinterpret_cast<Record*>(scq_->StartEnqueue());
CHECK(rec);
*rec = i;
scq_->FinishEnqueue();
}
finished_->Signal();
}
private:
TestSampleQueue* scq_;
const int records_per_chunk_;
Record value_;
v8::base::Semaphore* finished_;
};
} // namespace
TEST_F(CircularQueueTest, SamplingCircularQueueMultithreading) {
// Emulate multiple VM threads working 'one thread at a time.'
// This test enqueues data from different threads. This corresponds
// to the case of profiling under Linux, where signal handler that
// does sampling is called in the context of different VM threads.
const int kRecordsPerChunk = 4;
TestSampleQueue scq;
v8::base::Semaphore semaphore(0);
ProducerThread producer1(&scq, kRecordsPerChunk, 1, &semaphore);
ProducerThread producer2(&scq, kRecordsPerChunk, 10, &semaphore);
ProducerThread producer3(&scq, kRecordsPerChunk, 20, &semaphore);
CHECK(!scq.Peek());
CHECK(producer1.Start());
semaphore.Wait();
for (Record i = 1; i < 1 + kRecordsPerChunk; ++i) {
Record* rec = reinterpret_cast<Record*>(scq.Peek());
CHECK(rec);
CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
scq.Remove();
CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
}
CHECK(!scq.Peek());
CHECK(producer2.Start());
semaphore.Wait();
for (Record i = 10; i < 10 + kRecordsPerChunk; ++i) {
Record* rec = reinterpret_cast<Record*>(scq.Peek());
CHECK(rec);
CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
scq.Remove();
CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
}
CHECK(!scq.Peek());
CHECK(producer3.Start());
semaphore.Wait();
for (Record i = 20; i < 20 + kRecordsPerChunk; ++i) {
Record* rec = reinterpret_cast<Record*>(scq.Peek());
CHECK(rec);
CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
scq.Remove();
CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
}
CHECK(!scq.Peek());
}

View File

@ -0,0 +1,184 @@
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/profiler/strings-storage.h"
#include <cstdio>
#include "test/unittests/test-utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace v8 {
namespace internal {
using StringsStorageWithIsolate = TestWithIsolate;
bool StringEq(const char* left, const char* right) {
return strcmp(left, right) == 0;
}
TEST_F(StringsStorageWithIsolate, GetNameFromString) {
StringsStorage storage;
// One char strings are canonical on the v8 heap so use a 2 char string here.
DirectHandle<String> str =
isolate()->factory()->NewStringFromAsciiChecked("xy");
const char* stored_str = storage.GetName(*str);
CHECK(StringEq("xy", stored_str));
// The storage should de-duplicate the underlying char arrays and return the
// exact same pointer for equivalent input strings.
const char* stored_str_twice = storage.GetName(*str);
CHECK_EQ(stored_str, stored_str_twice);
// Even if the input string was a different one on the v8 heap, if the char
// array is the same, it should be de-duplicated.
DirectHandle<String> str2 =
isolate()->factory()->NewStringFromAsciiChecked("xy");
CHECK_NE(*str, *str2);
const char* stored_str_thrice = storage.GetName(*str2);
CHECK_EQ(stored_str_twice, stored_str_thrice);
}
TEST_F(StringsStorageWithIsolate, GetNameFromSymbol) {
StringsStorage storage;
DirectHandle<Symbol> symbol = isolate()->factory()->NewSymbol();
const char* stored_symbol = storage.GetName(*symbol);
CHECK(StringEq("<symbol>", stored_symbol));
DirectHandle<Symbol> symbol2 = isolate()->factory()->NewSymbol();
CHECK_NE(*symbol, *symbol2);
const char* stored_symbol2 = storage.GetName(*symbol2);
CHECK_EQ(stored_symbol, stored_symbol2);
}
TEST_F(StringsStorageWithIsolate, GetConsName) {
StringsStorage storage;
DirectHandle<String> str =
isolate()->factory()->NewStringFromAsciiChecked("xy");
const char* empty_prefix_str = storage.GetConsName("", *str);
CHECK(StringEq("xy", empty_prefix_str));
const char* get_str = storage.GetConsName("get ", *str);
CHECK(StringEq("get xy", get_str));
}
TEST_F(StringsStorageWithIsolate, GetNameFromInt) {
StringsStorage storage;
const char* stored_str = storage.GetName(0);
CHECK(StringEq("0", stored_str));
stored_str = storage.GetName(2147483647);
CHECK(StringEq("2147483647", stored_str));
stored_str = storage.GetName(std::numeric_limits<int>::min());
char str_negative_int[12];
snprintf(str_negative_int, sizeof(str_negative_int), "%d",
std::numeric_limits<int>::min());
CHECK(StringEq(str_negative_int, stored_str));
}
TEST_F(StringsStorageWithIsolate, Format) {
StringsStorage storage;
const char* xy = "xy";
const char* stored_str = storage.GetFormatted("%s", xy);
CHECK(StringEq("xy", stored_str));
// Check that the string is copied.
CHECK_NE(xy, stored_str);
const char* formatted_str = storage.GetFormatted("%s / %s", xy, xy);
CHECK(StringEq("xy / xy", formatted_str));
// A different format specifier that results in the same string should share
// the string in storage.
const char* formatted_str2 = storage.GetFormatted("%s", "xy / xy");
CHECK_EQ(formatted_str, formatted_str2);
}
TEST_F(StringsStorageWithIsolate, FormatAndGetShareStorage) {
StringsStorage storage;
DirectHandle<String> str =
isolate()->factory()->NewStringFromAsciiChecked("xy");
const char* stored_str = storage.GetName(*str);
const char* formatted_str = storage.GetFormatted("%s", "xy");
CHECK_EQ(stored_str, formatted_str);
}
TEST_F(StringsStorageWithIsolate, Refcounting) {
StringsStorage storage;
const char* a = storage.GetCopy("12");
CHECK_EQ(storage.GetStringCountForTesting(), 1);
CHECK_EQ(2, storage.GetStringSize());
const char* b = storage.GetCopy("12");
CHECK_EQ(storage.GetStringCountForTesting(), 1);
CHECK_EQ(2, storage.GetStringSize());
// Ensure that we deduplicate the string.
CHECK_EQ(a, b);
CHECK(storage.Release(a));
CHECK_EQ(storage.GetStringCountForTesting(), 1);
CHECK_EQ(2, storage.GetStringSize());
CHECK(storage.Release(b));
CHECK_EQ(storage.GetStringCountForTesting(), 0);
CHECK_EQ(0, storage.GetStringSize());
#if !DEBUG
CHECK(!storage.Release("12"));
#endif // !DEBUG
// Verify that other constructors refcount as intended.
const char* c = storage.GetFormatted("%d", 12);
CHECK_EQ(storage.GetStringCountForTesting(), 1);
CHECK_EQ(2, storage.GetStringSize());
const char* d = storage.GetName(12);
CHECK_EQ(storage.GetStringCountForTesting(), 1);
CHECK_EQ(2, storage.GetStringSize());
CHECK_EQ(c, d);
CHECK(storage.Release(c));
CHECK_EQ(storage.GetStringCountForTesting(), 1);
CHECK_EQ(2, storage.GetStringSize());
CHECK(storage.Release(d));
CHECK_EQ(storage.GetStringCountForTesting(), 0);
CHECK_EQ(0, storage.GetStringSize());
CHECK(!storage.Release("12"));
}
TEST_F(StringsStorageWithIsolate, InvalidRelease) {
StringsStorage storage;
// If we attempt to release a string not being managed by the StringsStorage,
// return false.
CHECK(!storage.Release("12"));
}
TEST_F(StringsStorageWithIsolate, CopyAndConsShareStorage) {
StringsStorage storage;
DirectHandle<String> str =
isolate()->factory()->NewStringFromAsciiChecked("foo");
const char* copy_str = storage.GetCopy("get foo");
const char* cons_str = storage.GetConsName("get ", *str);
CHECK_EQ(storage.GetStringCountForTesting(), 1);
CHECK_EQ(copy_str, cons_str);
}
} // namespace internal
} // namespace v8