scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
halcanary | c79a391 | 2015-04-01 13:31:34 -0700 | [diff] [blame] | 10 | #include "ProcStats.h" |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 11 | #include "SkCommonFlags.h" |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame] | 12 | #include "SkData.h" |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 13 | #include "SkJSONCPP.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 14 | #include "SkMutex.h" |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 15 | #include "SkOSFile.h" |
| 16 | #include "SkStream.h" |
| 17 | #include "SkTArray.h" |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 18 | |
| 19 | namespace DM { |
| 20 | |
| 21 | SkTArray<JsonWriter::BitmapResult> gBitmapResults; |
reed | 086eea9 | 2016-05-04 17:12:46 -0700 | [diff] [blame] | 22 | SK_DECLARE_STATIC_MUTEX(gBitmapResultLock); |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 23 | |
| 24 | void JsonWriter::AddBitmapResult(const BitmapResult& result) { |
| 25 | SkAutoMutexAcquire lock(&gBitmapResultLock); |
| 26 | gBitmapResults.push_back(result); |
| 27 | } |
| 28 | |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 29 | SkTArray<skiatest::Failure> gFailures; |
reed | 086eea9 | 2016-05-04 17:12:46 -0700 | [diff] [blame] | 30 | SK_DECLARE_STATIC_MUTEX(gFailureLock); |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 31 | |
| 32 | void JsonWriter::AddTestFailure(const skiatest::Failure& failure) { |
| 33 | SkAutoMutexAcquire lock(gFailureLock); |
| 34 | gFailures.push_back(failure); |
| 35 | } |
| 36 | |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 37 | void 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; |
mtklein | 409d470 | 2016-02-29 07:38:01 -0800 | [diff] [blame] | 55 | result["key"]["name"] = gBitmapResults[i].name.c_str(); |
| 56 | result["key"]["config"] = gBitmapResults[i].config.c_str(); |
| 57 | result["key"]["source_type"] = gBitmapResults[i].sourceType.c_str(); |
| 58 | result["options"]["ext"] = gBitmapResults[i].ext.c_str(); |
mtklein | b3f3148 | 2016-03-01 10:31:42 -0800 | [diff] [blame] | 59 | result["options"]["gamma_correct"] = gBitmapResults[i].gammaCorrect ? "yes" : "no"; |
mtklein | 409d470 | 2016-02-29 07:38:01 -0800 | [diff] [blame] | 60 | result["md5"] = gBitmapResults[i].md5.c_str(); |
mtklein | 20c1c04 | 2015-04-06 07:22:05 -0700 | [diff] [blame] | 61 | |
| 62 | // Source options only need to be part of the key if they exist. |
| 63 | // Source type by source type, we either always set options or never set options. |
| 64 | if (!gBitmapResults[i].sourceOptions.isEmpty()) { |
| 65 | result["key"]["source_options"] = gBitmapResults[i].sourceOptions.c_str(); |
| 66 | } |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 67 | |
| 68 | root["results"].append(result); |
| 69 | } |
| 70 | } |
| 71 | |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 72 | { |
| 73 | SkAutoMutexAcquire lock(gFailureLock); |
| 74 | for (int i = 0; i < gFailures.count(); i++) { |
| 75 | Json::Value result; |
| 76 | result["file_name"] = gFailures[i].fileName; |
| 77 | result["line_no"] = gFailures[i].lineNo; |
| 78 | result["condition"] = gFailures[i].condition; |
| 79 | result["message"] = gFailures[i].message.c_str(); |
| 80 | |
| 81 | root["test_results"]["failures"].append(result); |
| 82 | } |
| 83 | } |
| 84 | |
halcanary | c79a391 | 2015-04-01 13:31:34 -0700 | [diff] [blame] | 85 | int maxResidentSetSizeMB = sk_tools::getMaxResidentSetSizeMB(); |
| 86 | if (maxResidentSetSizeMB != -1) { |
| 87 | root["max_rss_MB"] = sk_tools::getMaxResidentSetSizeMB(); |
| 88 | } |
| 89 | |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 90 | SkString path = SkOSPath::Join(FLAGS_writePath[0], "dm.json"); |
halcanary | 022afb8 | 2015-01-30 11:00:12 -0800 | [diff] [blame] | 91 | sk_mkdir(FLAGS_writePath[0]); |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 92 | SkFILEWStream stream(path.c_str()); |
| 93 | stream.writeText(Json::StyledWriter().write(root).c_str()); |
| 94 | stream.flush(); |
| 95 | } |
| 96 | |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame] | 97 | bool JsonWriter::ReadJson(const char* path, void(*callback)(BitmapResult)) { |
| 98 | SkAutoTUnref<SkData> json(SkData::NewFromFileName(path)); |
| 99 | if (!json) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | Json::Reader reader; |
| 104 | Json::Value root; |
| 105 | const char* data = (const char*)json->data(); |
| 106 | if (!reader.parse(data, data+json->size(), root)) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | const Json::Value& results = root["results"]; |
| 111 | BitmapResult br; |
| 112 | for (unsigned i = 0; i < results.size(); i++) { |
| 113 | const Json::Value& r = results[i]; |
mtklein | 409d470 | 2016-02-29 07:38:01 -0800 | [diff] [blame] | 114 | br.name = r["key"]["name"].asCString(); |
| 115 | br.config = r["key"]["config"].asCString(); |
| 116 | br.sourceType = r["key"]["source_type"].asCString(); |
| 117 | br.ext = r["options"]["ext"].asCString(); |
mtklein | b3f3148 | 2016-03-01 10:31:42 -0800 | [diff] [blame] | 118 | br.gammaCorrect = 0 == strcmp("yes", r["options"]["gamma_correct"].asCString()); |
mtklein | 409d470 | 2016-02-29 07:38:01 -0800 | [diff] [blame] | 119 | br.md5 = r["md5"].asCString(); |
mtklein | 0ea6d71 | 2015-04-07 08:48:38 -0700 | [diff] [blame] | 120 | |
| 121 | if (!r["key"]["source_options"].isNull()) { |
| 122 | br.sourceOptions = r["key"]["source_options"].asCString(); |
| 123 | } |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame] | 124 | callback(br); |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 129 | } // namespace DM |