2017-03-21 17:14:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// ok is an experimental test harness, maybe to replace DM. Key features:
|
|
|
|
// * work is balanced across separate processes for stability and isolation;
|
|
|
|
// * ok is entirely opt-in. No more maintaining huge --blacklists.
|
|
|
|
|
|
|
|
#include "SkGraphics.h"
|
2017-05-03 19:16:58 +00:00
|
|
|
#include "SkImage.h"
|
2017-03-25 15:29:41 +00:00
|
|
|
#include "ok.h"
|
2017-03-23 19:29:26 +00:00
|
|
|
#include <chrono>
|
2017-03-21 17:14:33 +00:00
|
|
|
#include <future>
|
2017-03-23 19:29:26 +00:00
|
|
|
#include <list>
|
2017-03-21 17:14:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <thread>
|
2017-03-29 16:41:13 +00:00
|
|
|
#include <vector>
|
2017-03-21 17:14:33 +00:00
|
|
|
|
2017-03-23 22:36:39 +00:00
|
|
|
#if !defined(__has_include)
|
|
|
|
#define __has_include(x) 0
|
|
|
|
#endif
|
|
|
|
|
2017-03-29 16:41:13 +00:00
|
|
|
static thread_local const char* tls_currently_running = "";
|
2017-03-23 22:36:39 +00:00
|
|
|
|
|
|
|
#if __has_include(<execinfo.h>) && __has_include(<fcntl.h>) && __has_include(<signal.h>)
|
|
|
|
#include <execinfo.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2017-03-28 13:30:11 +00:00
|
|
|
static int log_fd = 2/*stderr*/;
|
|
|
|
|
|
|
|
static void log(const char* msg) {
|
|
|
|
write(log_fd, msg, strlen(msg));
|
|
|
|
}
|
2017-03-23 22:36:39 +00:00
|
|
|
|
|
|
|
static void setup_crash_handler() {
|
|
|
|
static void (*original_handlers[32])(int);
|
|
|
|
for (int sig : std::vector<int>{ SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV }) {
|
|
|
|
original_handlers[sig] = signal(sig, [](int sig) {
|
2017-03-28 13:30:11 +00:00
|
|
|
lockf(log_fd, F_LOCK, 0);
|
|
|
|
log("\ncaught signal ");
|
|
|
|
switch (sig) {
|
|
|
|
#define CASE(s) case s: log(#s); break
|
|
|
|
CASE(SIGABRT);
|
|
|
|
CASE(SIGBUS);
|
|
|
|
CASE(SIGFPE);
|
|
|
|
CASE(SIGILL);
|
|
|
|
CASE(SIGSEGV);
|
|
|
|
#undef CASE
|
|
|
|
}
|
|
|
|
log(" while running '");
|
2017-03-29 16:41:13 +00:00
|
|
|
log(tls_currently_running);
|
2017-03-28 13:30:11 +00:00
|
|
|
log("'\n");
|
|
|
|
|
|
|
|
void* stack[128];
|
|
|
|
int frames = backtrace(stack, sizeof(stack)/sizeof(*stack));
|
|
|
|
backtrace_symbols_fd(stack, frames, log_fd);
|
|
|
|
lockf(log_fd, F_ULOCK, 0);
|
2017-03-23 22:36:39 +00:00
|
|
|
|
|
|
|
signal(sig, original_handlers[sig]);
|
|
|
|
raise(sig);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 13:30:11 +00:00
|
|
|
static void defer_logging() {
|
|
|
|
log_fd = fileno(tmpfile());
|
2017-03-23 22:36:39 +00:00
|
|
|
atexit([] {
|
2017-03-28 13:30:11 +00:00
|
|
|
lseek(log_fd, 0, SEEK_SET);
|
2017-03-23 22:36:39 +00:00
|
|
|
char buf[1024];
|
2017-03-28 13:30:11 +00:00
|
|
|
while (size_t bytes = read(log_fd, buf, sizeof(buf))) {
|
2017-03-23 22:36:39 +00:00
|
|
|
write(2, buf, bytes);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-03-28 13:30:11 +00:00
|
|
|
|
|
|
|
void ok_log(const char* msg) {
|
|
|
|
lockf(log_fd, F_LOCK, 0);
|
|
|
|
log("[");
|
2017-03-29 16:41:13 +00:00
|
|
|
log(tls_currently_running);
|
2017-03-28 13:30:11 +00:00
|
|
|
log("]\t");
|
|
|
|
log(msg);
|
|
|
|
log("\n");
|
|
|
|
lockf(log_fd, F_ULOCK, 0);
|
|
|
|
}
|
|
|
|
|
2017-03-23 22:36:39 +00:00
|
|
|
#else
|
|
|
|
static void setup_crash_handler() {}
|
2017-03-28 13:30:11 +00:00
|
|
|
static void defer_logging() {}
|
|
|
|
|
|
|
|
void ok_log(const char* msg) {
|
|
|
|
fprintf(stderr, "%s\n", msg);
|
|
|
|
}
|
2017-03-23 22:36:39 +00:00
|
|
|
#endif
|
|
|
|
|
2017-03-21 17:14:33 +00:00
|
|
|
struct Engine {
|
|
|
|
virtual ~Engine() {}
|
|
|
|
virtual bool spawn(std::function<Status(void)>) = 0;
|
|
|
|
virtual Status wait_one() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SerialEngine : Engine {
|
|
|
|
Status last = Status::None;
|
|
|
|
|
|
|
|
bool spawn(std::function<Status(void)> fn) override {
|
|
|
|
last = fn();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status wait_one() override {
|
|
|
|
Status s = last;
|
|
|
|
last = Status::None;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ThreadEngine : Engine {
|
2017-03-23 19:29:26 +00:00
|
|
|
std::list<std::future<Status>> live;
|
2017-04-27 13:50:34 +00:00
|
|
|
const std::chrono::steady_clock::time_point the_past = std::chrono::steady_clock::now();
|
2017-03-21 17:14:33 +00:00
|
|
|
|
|
|
|
bool spawn(std::function<Status(void)> fn) override {
|
|
|
|
live.push_back(std::async(std::launch::async, fn));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status wait_one() override {
|
|
|
|
if (live.empty()) {
|
|
|
|
return Status::None;
|
|
|
|
}
|
2017-03-23 19:29:26 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
for (auto it = live.begin(); it != live.end(); it++) {
|
2017-04-27 13:50:34 +00:00
|
|
|
if (it->wait_until(the_past) == std::future_status::ready) {
|
2017-03-23 19:29:26 +00:00
|
|
|
Status s = it->get();
|
|
|
|
live.erase(it);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-21 17:14:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
using ForkEngine = ThreadEngine;
|
|
|
|
#else
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
struct ForkEngine : Engine {
|
|
|
|
bool spawn(std::function<Status(void)> fn) override {
|
|
|
|
switch (fork()) {
|
2017-03-23 22:36:39 +00:00
|
|
|
case 0: _exit((int)fn());
|
2017-03-21 17:14:33 +00:00
|
|
|
case -1: return false;
|
|
|
|
default: return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status wait_one() override {
|
|
|
|
do {
|
|
|
|
int status;
|
|
|
|
if (wait(&status) > 0) {
|
|
|
|
return WIFEXITED(status) ? (Status)WEXITSTATUS(status)
|
|
|
|
: Status::Crashed;
|
|
|
|
}
|
|
|
|
} while (errno == EINTR);
|
|
|
|
return Status::None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2017-03-25 15:29:41 +00:00
|
|
|
struct StreamType {
|
2017-03-29 16:41:13 +00:00
|
|
|
const char *name, *help;
|
2017-03-21 17:14:33 +00:00
|
|
|
std::unique_ptr<Stream> (*factory)(Options);
|
|
|
|
};
|
2017-03-25 15:29:41 +00:00
|
|
|
static std::vector<StreamType> stream_types;
|
2017-03-21 17:14:33 +00:00
|
|
|
|
2017-03-25 15:29:41 +00:00
|
|
|
struct DstType {
|
2017-03-29 16:41:13 +00:00
|
|
|
const char *name, *help;
|
2017-03-25 19:53:14 +00:00
|
|
|
std::unique_ptr<Dst> (*factory)(Options);
|
2017-03-24 18:06:47 +00:00
|
|
|
};
|
2017-03-25 15:29:41 +00:00
|
|
|
static std::vector<DstType> dst_types;
|
2017-03-24 18:06:47 +00:00
|
|
|
|
2017-03-25 16:32:22 +00:00
|
|
|
struct ViaType {
|
2017-03-29 16:41:13 +00:00
|
|
|
const char *name, *help;
|
2017-03-25 19:53:14 +00:00
|
|
|
std::unique_ptr<Dst> (*factory)(Options, std::unique_ptr<Dst>);
|
2017-03-25 16:32:22 +00:00
|
|
|
};
|
|
|
|
static std::vector<ViaType> via_types;
|
|
|
|
|
2017-03-29 16:41:13 +00:00
|
|
|
template <typename T>
|
|
|
|
static std::string help_for(std::vector<T> registered) {
|
|
|
|
std::string help;
|
|
|
|
for (auto r : registered) {
|
|
|
|
help += "\n ";
|
|
|
|
help += r.name;
|
|
|
|
help += ": ";
|
|
|
|
help += r.help;
|
|
|
|
}
|
|
|
|
return help;
|
|
|
|
}
|
|
|
|
|
2017-03-21 17:14:33 +00:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
SkGraphics::Init();
|
2017-03-23 22:36:39 +00:00
|
|
|
setup_crash_handler();
|
2017-03-21 17:14:33 +00:00
|
|
|
|
2017-03-29 16:41:13 +00:00
|
|
|
int jobs{1};
|
2017-03-25 19:53:14 +00:00
|
|
|
std::unique_ptr<Stream> stream;
|
2017-03-29 16:41:13 +00:00
|
|
|
std::function<std::unique_ptr<Dst>(void)> dst_factory = []{
|
|
|
|
// A default Dst that's enough for unit tests and not much else.
|
|
|
|
struct : Dst {
|
|
|
|
Status draw(Src* src) override { return src->draw(nullptr); }
|
|
|
|
sk_sp<SkImage> image() override { return nullptr; }
|
|
|
|
} dst;
|
|
|
|
return move_unique(dst);
|
|
|
|
};
|
2017-03-21 17:14:33 +00:00
|
|
|
|
|
|
|
auto help = [&] {
|
2017-03-29 16:41:13 +00:00
|
|
|
std::string stream_help = help_for(stream_types),
|
|
|
|
dst_help = help_for( dst_types),
|
|
|
|
via_help = help_for( via_types);
|
2017-03-21 17:14:33 +00:00
|
|
|
|
2017-04-12 15:02:44 +00:00
|
|
|
printf("%s [-j N] src[:k=v,...] dst[:k=v,...] [via[:k=v,...] ...] \n"
|
2017-03-25 16:32:22 +00:00
|
|
|
" -j: Run at most N processes at any time. \n"
|
|
|
|
" If <0, use -N threads instead. \n"
|
|
|
|
" If 0, use one thread in one process. \n"
|
|
|
|
" If 1 (default) or -1, auto-detect N. \n"
|
2017-03-29 16:41:13 +00:00
|
|
|
" src: content to draw%s \n"
|
|
|
|
" dst: how to draw that content%s \n"
|
|
|
|
" via: wrappers around dst%s \n"
|
|
|
|
" Most srcs, dsts and vias have options, e.g. skp:dir=skps sw:ct=565 \n",
|
|
|
|
argv[0], stream_help.c_str(), dst_help.c_str(), via_help.c_str());
|
2017-03-21 17:14:33 +00:00
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; i++) {
|
2017-04-12 15:02:44 +00:00
|
|
|
if (0 == strcmp("-j", argv[i])) { jobs = atoi(argv[++i]); }
|
|
|
|
if (0 == strcmp("-h", argv[i])) { return help(); }
|
|
|
|
if (0 == strcmp("--help", argv[i])) { return help(); }
|
2017-03-21 17:14:33 +00:00
|
|
|
|
2017-03-25 15:29:41 +00:00
|
|
|
for (auto s : stream_types) {
|
2017-03-24 18:06:47 +00:00
|
|
|
size_t len = strlen(s.name);
|
|
|
|
if (0 == strncmp(s.name, argv[i], len)) {
|
2017-03-21 17:14:33 +00:00
|
|
|
switch (argv[i][len]) {
|
|
|
|
case ':': len++;
|
2017-03-24 18:06:47 +00:00
|
|
|
case '\0': stream = s.factory(Options{argv[i]+len});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-25 15:29:41 +00:00
|
|
|
for (auto d : dst_types) {
|
2017-03-24 18:06:47 +00:00
|
|
|
size_t len = strlen(d.name);
|
|
|
|
if (0 == strncmp(d.name, argv[i], len)) {
|
|
|
|
switch (argv[i][len]) {
|
|
|
|
case ':': len++;
|
2017-03-25 19:53:14 +00:00
|
|
|
case '\0': dst_factory = [=]{
|
|
|
|
return d.factory(Options{argv[i]+len});
|
2017-03-25 16:32:22 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto v : via_types) {
|
|
|
|
size_t len = strlen(v.name);
|
|
|
|
if (0 == strncmp(v.name, argv[i], len)) {
|
2017-03-25 19:53:14 +00:00
|
|
|
if (!dst_factory) { return help(); }
|
2017-03-25 16:32:22 +00:00
|
|
|
switch (argv[i][len]) {
|
|
|
|
case ':': len++;
|
2017-03-25 19:53:14 +00:00
|
|
|
case '\0': dst_factory = [=]{
|
|
|
|
return v.factory(Options{argv[i]+len}, dst_factory());
|
2017-03-25 15:29:41 +00:00
|
|
|
};
|
2017-03-21 17:14:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-27 18:16:04 +00:00
|
|
|
if (!stream) { return help(); }
|
2017-03-21 17:14:33 +00:00
|
|
|
|
|
|
|
std::unique_ptr<Engine> engine;
|
2017-03-28 13:30:11 +00:00
|
|
|
if (jobs == 0) { engine.reset(new SerialEngine); }
|
|
|
|
if (jobs > 0) { engine.reset(new ForkEngine); defer_logging(); }
|
|
|
|
if (jobs < 0) { engine.reset(new ThreadEngine); jobs = -jobs; }
|
2017-03-21 17:14:33 +00:00
|
|
|
|
|
|
|
if (jobs == 1) { jobs = std::thread::hardware_concurrency(); }
|
|
|
|
|
|
|
|
int ok = 0, failed = 0, crashed = 0, skipped = 0;
|
|
|
|
|
|
|
|
auto update_stats = [&](Status s) {
|
|
|
|
switch (s) {
|
|
|
|
case Status::OK: ok++; break;
|
|
|
|
case Status::Failed: failed++; break;
|
|
|
|
case Status::Crashed: crashed++; break;
|
|
|
|
case Status::Skipped: skipped++; break;
|
|
|
|
case Status::None: return;
|
|
|
|
}
|
|
|
|
const char* leader = "\r";
|
|
|
|
auto print = [&](int count, const char* label) {
|
|
|
|
if (count) {
|
|
|
|
printf("%s%d %s", leader, count, label);
|
|
|
|
leader = ", ";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
print(ok, "ok");
|
|
|
|
print(failed, "failed");
|
|
|
|
print(crashed, "crashed");
|
|
|
|
print(skipped, "skipped");
|
|
|
|
fflush(stdout);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto spawn = [&](std::function<Status(void)> fn) {
|
|
|
|
if (--jobs < 0) {
|
|
|
|
update_stats(engine->wait_one());
|
|
|
|
}
|
|
|
|
while (!engine->spawn(fn)) {
|
|
|
|
update_stats(engine->wait_one());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (std::unique_ptr<Src> owned = stream->next(); owned; owned = stream->next()) {
|
|
|
|
Src* raw = owned.release(); // Can't move std::unique_ptr into a lambda in C++11. :(
|
|
|
|
spawn([=] {
|
|
|
|
std::unique_ptr<Src> src{raw};
|
|
|
|
|
2017-03-29 16:41:13 +00:00
|
|
|
std::string name = src->name();
|
|
|
|
tls_currently_running = name.c_str();
|
ok: fix an assert from unbalanced save/restores
The 'dont_clip_to_layer' GM does not balance its layers. It calls
saveLayer() 3x but restore() only 2x. This may be a bug in the GM
itself, but I'm not sure so I haven't changed it here.
If ok is writing .pngs, the surface passes ownership of its buffer to an
image snapshot, simply marking it as immutable. Then, when the surface
is destroyed later, it destroys its inner canvas, which restores its
save stack to zero, actually doing some drawing in the case of
unbalanced saveLayer()s. We then call notifyPixelsChanged() and hit an
assert saying "you just wrote some pixels but this buffer was marked
immutable."
DM doesn't show this problem because it's doesn't really use surfaces
and images, just bitmaps. There's no ownership handoff and nothing is
ever immutable, so the condition triggering the assert never comes up.
I'm not really sure where we want to say is the bug:
- SkCanvas can draw in its destructor?
- SkSurface doesn't restore to zero before snapping an image?
- that dont_clip_to_layer should call restore three times?
In any case, this guards against it in ok.
I was using this as a convenient crash to help figure out how to best
save and print stack traces, but now that I've got that worked out we
might as well fix this.
Change-Id: Id6d397f534dd1b50219e0d3078c989a4910883a6
Reviewed-on: https://skia-review.googlesource.com/10140
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Mike Klein <mtklein@chromium.org>
2017-03-23 22:57:59 +00:00
|
|
|
|
2017-03-29 16:41:13 +00:00
|
|
|
return dst_factory()->draw(src.get());
|
2017-03-21 17:14:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Status s = Status::OK; s != Status::None; ) {
|
|
|
|
s = engine->wait_one();
|
|
|
|
update_stats(s);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
return (failed || crashed) ? 1 : 0;
|
|
|
|
}
|
2017-03-25 15:29:41 +00:00
|
|
|
|
|
|
|
|
2017-03-29 16:41:13 +00:00
|
|
|
Register::Register(const char* name, const char* help,
|
|
|
|
std::unique_ptr<Stream> (*factory)(Options)) {
|
|
|
|
stream_types.push_back(StreamType{name, help, factory});
|
2017-03-25 15:29:41 +00:00
|
|
|
}
|
2017-03-29 16:41:13 +00:00
|
|
|
Register::Register(const char* name, const char* help,
|
|
|
|
std::unique_ptr<Dst> (*factory)(Options)) {
|
|
|
|
dst_types.push_back(DstType{name, help, factory});
|
2017-03-25 15:29:41 +00:00
|
|
|
}
|
2017-03-29 16:41:13 +00:00
|
|
|
Register::Register(const char* name, const char* help,
|
2017-03-25 19:53:14 +00:00
|
|
|
std::unique_ptr<Dst> (*factory)(Options, std::unique_ptr<Dst>)) {
|
2017-03-29 16:41:13 +00:00
|
|
|
via_types.push_back(ViaType{name, help, factory});
|
2017-03-25 16:32:22 +00:00
|
|
|
}
|
2017-03-25 15:29:41 +00:00
|
|
|
|
|
|
|
Options::Options(std::string str) {
|
|
|
|
std::string k,v, *curr = &k;
|
|
|
|
for (auto c : str) {
|
|
|
|
switch(c) {
|
2017-03-27 16:43:44 +00:00
|
|
|
case ',': (*this)[k] = v;
|
2017-03-25 15:29:41 +00:00
|
|
|
curr = &(k = "");
|
|
|
|
break;
|
|
|
|
case '=': curr = &(v = "");
|
|
|
|
break;
|
|
|
|
default: *curr += c;
|
|
|
|
}
|
|
|
|
}
|
2017-03-27 16:43:44 +00:00
|
|
|
(*this)[k] = v;
|
2017-03-25 15:29:41 +00:00
|
|
|
}
|
|
|
|
|
2017-03-27 16:43:44 +00:00
|
|
|
std::string& Options::operator[](std::string k) { return this->kv[k]; }
|
|
|
|
|
2017-03-25 15:29:41 +00:00
|
|
|
std::string Options::operator()(std::string k, std::string fallback) const {
|
|
|
|
for (auto it = kv.find(k); it != kv.end(); ) {
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
return fallback;
|
|
|
|
}
|