blob: c017dde5b56c31c9e0ac3a377eecd504d372d9fe [file] [log] [blame]
scroggo7a10fb62014-11-04 07:21:10 -08001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "DMJsonWriter.h"
9
halcanaryc79a3912015-04-01 13:31:34 -070010#include "ProcStats.h"
scroggo7a10fb62014-11-04 07:21:10 -080011#include "SkCommonFlags.h"
mtklein62bd1a62015-01-27 14:46:26 -080012#include "SkData.h"
scroggo7a10fb62014-11-04 07:21:10 -080013#include "SkJSONCPP.h"
14#include "SkOSFile.h"
15#include "SkStream.h"
16#include "SkTArray.h"
17#include "SkThread.h"
18
19namespace DM {
20
21SkTArray<JsonWriter::BitmapResult> gBitmapResults;
22SK_DECLARE_STATIC_MUTEX(gBitmapResultLock);
23
24void JsonWriter::AddBitmapResult(const BitmapResult& result) {
25 SkAutoMutexAcquire lock(&gBitmapResultLock);
26 gBitmapResults.push_back(result);
27}
28
scroggo0ee26272014-11-07 06:07:32 -080029SkTArray<skiatest::Failure> gFailures;
30SK_DECLARE_STATIC_MUTEX(gFailureLock);
31
32void JsonWriter::AddTestFailure(const skiatest::Failure& failure) {
33 SkAutoMutexAcquire lock(gFailureLock);
34 gFailures.push_back(failure);
35}
36
scroggo7a10fb62014-11-04 07:21:10 -080037void JsonWriter::DumpJson() {
38 if (FLAGS_writePath.isEmpty()) {
39 return;
40 }
41
42 Json::Value root;
43
44 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
45 root[FLAGS_properties[i-1]] = FLAGS_properties[i];
46 }
47 for (int i = 1; i < FLAGS_key.count(); i += 2) {
48 root["key"][FLAGS_key[i-1]] = FLAGS_key[i];
49 }
50
51 {
52 SkAutoMutexAcquire lock(&gBitmapResultLock);
53 for (int i = 0; i < gBitmapResults.count(); i++) {
54 Json::Value result;
mtklein3d8878c2015-01-05 06:47:41 -080055 result["key"]["name"] = gBitmapResults[i].name.c_str();
56 result["key"]["config"] = gBitmapResults[i].config.c_str();
mtklein3d8878c2015-01-05 06:47:41 -080057 result["key"]["source_type"] = gBitmapResults[i].sourceType.c_str();
mtklein82079fb2015-02-17 13:18:04 -080058 result["options"]["ext"] = gBitmapResults[i].ext.c_str();
mtklein3d8878c2015-01-05 06:47:41 -080059 result["md5"] = gBitmapResults[i].md5.c_str();
scroggo7a10fb62014-11-04 07:21:10 -080060
61 root["results"].append(result);
62 }
63 }
64
scroggo0ee26272014-11-07 06:07:32 -080065 {
66 SkAutoMutexAcquire lock(gFailureLock);
67 for (int i = 0; i < gFailures.count(); i++) {
68 Json::Value result;
69 result["file_name"] = gFailures[i].fileName;
70 result["line_no"] = gFailures[i].lineNo;
71 result["condition"] = gFailures[i].condition;
72 result["message"] = gFailures[i].message.c_str();
73
74 root["test_results"]["failures"].append(result);
75 }
76 }
77
halcanaryc79a3912015-04-01 13:31:34 -070078 int maxResidentSetSizeMB = sk_tools::getMaxResidentSetSizeMB();
79 if (maxResidentSetSizeMB != -1) {
80 root["max_rss_MB"] = sk_tools::getMaxResidentSetSizeMB();
81 }
82
scroggo7a10fb62014-11-04 07:21:10 -080083 SkString path = SkOSPath::Join(FLAGS_writePath[0], "dm.json");
halcanary022afb82015-01-30 11:00:12 -080084 sk_mkdir(FLAGS_writePath[0]);
scroggo7a10fb62014-11-04 07:21:10 -080085 SkFILEWStream stream(path.c_str());
86 stream.writeText(Json::StyledWriter().write(root).c_str());
87 stream.flush();
88}
89
mtklein62bd1a62015-01-27 14:46:26 -080090bool JsonWriter::ReadJson(const char* path, void(*callback)(BitmapResult)) {
91 SkAutoTUnref<SkData> json(SkData::NewFromFileName(path));
92 if (!json) {
93 return false;
94 }
95
96 Json::Reader reader;
97 Json::Value root;
98 const char* data = (const char*)json->data();
99 if (!reader.parse(data, data+json->size(), root)) {
100 return false;
101 }
102
103 const Json::Value& results = root["results"];
104 BitmapResult br;
105 for (unsigned i = 0; i < results.size(); i++) {
106 const Json::Value& r = results[i];
107 br.name = r["key"]["name"].asCString();
108 br.config = r["key"]["config"].asCString();
109 br.sourceType = r["key"]["source_type"].asCString();
mtklein82079fb2015-02-17 13:18:04 -0800110 br.ext = r["options"]["ext"].asCString();
mtklein62bd1a62015-01-27 14:46:26 -0800111 br.md5 = r["md5"].asCString();
112 callback(br);
113 }
114 return true;
115}
116
scroggo7a10fb62014-11-04 07:21:10 -0800117} // namespace DM