memory_kit.dart 12.4 KB
Newer Older
[
[linjizong] 已提交
1 2
import 'dart:ui';

3 4 5
import 'package:dokit/dokit.dart';
import 'package:dokit/kit/apm/apm.dart';
import 'package:dokit/kit/apm/vm/vm_helper.dart';
6
import 'package:dokit/kit/apm/vm/vm_service_wrapper.dart';
7
import 'package:dokit/kit/kit.dart';
[
[linjizong] 已提交
8
import 'package:dokit/util/byte_util.dart';
[
[linjizong] 已提交
9 10 11 12 13 14
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:vm_service/vm_service.dart';

class MemoryInfo implements IInfo {
J
JellyBean 已提交
15 16
  int? fps;
  String? pageName;
[
[linjizong] 已提交
17 18

  @override
J
JellyBean 已提交
19
  int? getValue() {
[
[linjizong] 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    return fps;
  }
}

class MemoryKit extends ApmKit {
  int lastFrame = 0;

  @override
  String getKitName() {
    return ApmKitName.KIT_MEMORY;
  }

  @override
  String getIcon() {
    return 'images/dk_ram.png';
  }

  @override
38
  void start() {
39
    VMServiceWrapper.instance.connect();
[
[linjizong] 已提交
40
    VmHelper vmHelper = VmHelper.instance;
41 42 43
    VMServiceWrapper.instance
        .connect()
        .then((value) => vmHelper.resolveVMInfo());
[
[linjizong] 已提交
44 45 46
  }

  void update() {
47 48
    VmHelper.instance.updateAllocationProfile();
    VmHelper.instance.updateFlutterVersion();
[
[linjizong] 已提交
49 50 51
    VmHelper.instance.updateMemoryUsage();
  }

J
JellyBean 已提交
52
  AllocationProfile? getAllocationProfile() {
[
[linjizong] 已提交
53 54 55 56 57
    return VmHelper.instance.allocationProfile;
  }

  @override
  void stop() {
58
    VMServiceWrapper.instance.disConnect();
[
[linjizong] 已提交
59 60 61 62 63 64 65 66 67
  }

  @override
  IStorage createStorage() {
    return CommonStorage(maxCount: 120);
  }

  @override
  Widget createDisplayPage() {
68
    return MemoryPage();
[
[linjizong] 已提交
69 70 71 72 73 74 75 76 77 78 79
  }
}

class MemoryPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MemoryPageState();
  }
}

class MemoryPageState extends State<MemoryPage> {
J
JellyBean 已提交
80
  MemoryKit? kit =
[
[linjizong] 已提交
81
      ApmKitManager.instance.getKit<MemoryKit>(ApmKitName.KIT_MEMORY);
J
JellyBean 已提交
82
  List<ClassHeapStats> heaps = [];
[
[linjizong] 已提交
83 84 85 86 87
  TextEditingController editingController = TextEditingController();

  @override
  void initState() {
    super.initState();
J
JellyBean 已提交
88
    kit?.update();
89 90 91 92
    initHeaps();
  }

  void initHeaps() {
J
JellyBean 已提交
93 94 95 96
    if (kit?.getAllocationProfile() != null) {
      kit!.getAllocationProfile()?.members?.sort((left, right) =>
          right.bytesCurrent?.compareTo(left.bytesCurrent ?? 0) ?? 0);
      kit?.getAllocationProfile()?.members?.forEach((element) {
97 98 99 100 101
        if (heaps.length < 32) {
          heaps.add(element);
        }
      });
    }
[
[linjizong] 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
        child: Container(
            margin: EdgeInsets.all(16),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text('Memory Info',
                          style: TextStyle(
                              color: Color(0xff333333),
                              fontWeight: FontWeight.bold,
                              fontSize: 16)),
121 122
                      StreamBuilder(
                        stream: Stream.periodic(Duration(seconds: 2), (value) {
123
                          VmHelper.instance.updateAllocationProfile();
124 125 126 127 128 129
                          VmHelper.instance.updateMemoryUsage();
                        }),
                        builder: (context, snapshot) {
                          return Container(
                            margin: EdgeInsets.only(top: 3),
                            alignment: Alignment.topLeft,
L
linusflow 已提交
130
                            child: VmHelper.instance.memoryInfo.isNotEmpty
131 132 133 134 135 136 137 138 139 140
                                ? Column(
                                    children: getMemoryInfo(
                                        VmHelper.instance.memoryInfo))
                                : Text('获取Memory数据失败(release模式下无法获取数据)',
                                    style: TextStyle(
                                        color: Color(0xff999999),
                                        fontSize: 12)),
                          );
                        },
                      )
[
[linjizong] 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
                    ]),
                Container(
                  margin: EdgeInsets.only(top: 10),
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.only(left: 13),
                  height: 50,
                  decoration: BoxDecoration(
                    border: Border.all(
                        color: Color(0xff337cc4),
                        width: 0.5,
                        style: BorderStyle.solid),
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Container(
                        child: TextField(
                          controller: editingController,
                          style:
                              TextStyle(color: Color(0xff333333), fontSize: 16),
                          inputFormatters: [
L
linusflow 已提交
163
                            FilteringTextInputFormatter.deny(RegExp(
[
[linjizong] 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177
                                '[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]'))
                          ],
                          onSubmitted: (value) => {filterAllocations()},
                          decoration: InputDecoration(
                            border: InputBorder.none,
                            hintStyle: TextStyle(
                                color: Color(0xffbebebe), fontSize: 16),
                            hintText: '输入类名,查看内存占用',
                          ),
                        ),
                        width: MediaQuery.of(context).size.width - 150,
                      ),
                      Container(
                        width: 60,
L
linusflow 已提交
178 179 180 181 182
                        child: TextButton(
                          style: ButtonStyle(
                            padding: MaterialStateProperty.all(EdgeInsets.only(
                                left: 15, right: 0, top: 15, bottom: 15)),
                          ),
[
[linjizong] 已提交
183
                          child: Image.asset('images/dk_memory_search.png',
J
JellyBean 已提交
184
                              package: DK_PACKAGE_NAME, height: 16, width: 16),
[
[linjizong] 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
                          onPressed: filterAllocations,
                        ),
                      )
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 12),
                  height: 34,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: <Widget>[
                      Container(
                        width: 80,
                        decoration: BoxDecoration(
                            color: Color(0xff337cc4),
                            borderRadius: BorderRadius.only(
                                topLeft: Radius.circular(4),
                                bottomLeft: Radius.circular(4))),
                        alignment: Alignment.center,
                        child: Text('Size',
                            style: TextStyle(
                                color: Color(0xffffffff), fontSize: 14)),
                      ),
                      VerticalDivider(
                        width: 0.5,
                        color: Color(0xffffffff),
                      ),
                      Container(
                        width: 80,
                        decoration: BoxDecoration(
                          color: Color(0xff337cc4),
                        ),
                        alignment: Alignment.center,
                        child: Text('Count',
                            style: TextStyle(
                                color: Color(0xffffffff), fontSize: 14)),
                      ),
                      VerticalDivider(
                        width: 0.5,
                        color: Color(0xffffffff),
                      ),
                      Container(
                          decoration: BoxDecoration(
                              color: Color(0xff337cc4),
                              borderRadius: BorderRadius.only(
                                  topRight: Radius.circular(4),
                                  bottomRight: Radius.circular(4))),
                          width: MediaQuery.of(context).size.width - 193,
                          alignment: Alignment.center,
                          child: Text('ClassName',
                              style: TextStyle(
                                  color: Color(0xffffffff), fontSize: 14))),
                    ],
                  ),
                ),
                Container(
                  height: MediaQuery.of(context).size.height - 200 - 210,
                  child: ListView.builder(
                      padding: EdgeInsets.all(0),
                      itemCount: heaps.length,
                      itemBuilder: (context, index) {
                        return HeapItemWidget(
                          item: heaps[index],
                          index: index,
                        );
                      }),
                ),
              ],
            )));
  }

  void filterAllocations() {
    String className = editingController.text;
    heaps.clear();
J
JellyBean 已提交
260 261 262 263 264 265
    if (className.length >= 3 && kit?.getAllocationProfile() != null) {
      kit?.getAllocationProfile()?.members?.forEach((element) {
        if (element.classRef?.name
                ?.toLowerCase()
                .contains(className.toLowerCase()) ==
            true) {
[
[linjizong] 已提交
266 267 268
          heaps.add(element);
        }
      });
J
JellyBean 已提交
269 270
      heaps.sort((left, right) =>
          right.bytesCurrent?.compareTo(left.bytesCurrent ?? 0) ?? 0);
[
[linjizong] 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    }
    setState(() {});
  }

  List<Widget> getMemoryInfo(Map<IsolateRef, MemoryUsage> map) {
    List<Widget> widgets = <Widget>[];
    map.forEach((key, value) {
      widgets.add(RichText(
          text: TextSpan(children: [
        TextSpan(
            text: 'IsolateName: ',
            style:
                TextStyle(fontSize: 10, color: Color(0xff333333), height: 1.5)),
        TextSpan(
            text: '${key.name}',
            style:
                TextStyle(fontSize: 10, height: 1.5, color: Color(0xff666666))),
        TextSpan(
            text: '\nHeapUsage: ',
            style:
                TextStyle(height: 1.5, fontSize: 10, color: Color(0xff333333))),
        TextSpan(
[
[linjizong] 已提交
293
            text: '${toByteString(value.heapUsage)}',
[
[linjizong] 已提交
294 295 296 297 298 299 300
            style:
                TextStyle(fontSize: 10, height: 1.5, color: Color(0xff666666))),
        TextSpan(
            text: '\nHeapCapacity: ',
            style:
                TextStyle(fontSize: 10, height: 1.5, color: Color(0xff333333))),
        TextSpan(
[
[linjizong] 已提交
301
            text: '${toByteString(value.heapCapacity)}',
[
[linjizong] 已提交
302 303 304 305 306 307 308
            style:
                TextStyle(fontSize: 10, height: 1.5, color: Color(0xff666666))),
        TextSpan(
            text: '\nExternalUsage: ',
            style:
                TextStyle(fontSize: 10, height: 1.5, color: Color(0xff333333))),
        TextSpan(
[
[linjizong] 已提交
309
            text: '${toByteString(value.externalUsage)}',
[
[linjizong] 已提交
310 311 312 313 314 315 316 317 318 319 320 321
            style:
                TextStyle(fontSize: 10, height: 1.5, color: Color(0xff666666))),
      ])));
    });
    return widgets;
  }
}

class HeapItemWidget extends StatelessWidget {
  final ClassHeapStats item;
  final int index;

J
JellyBean 已提交
322
  HeapItemWidget({Key? key, required this.item, required this.index})
[
[linjizong] 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 40,
      color: index % 2 == 1 ? Color(0xfffafafa) : Colors.white,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Container(
            width: 80,
            alignment: Alignment.center,
[
[linjizong] 已提交
336
            child: Text('${toByteString(item.bytesCurrent)}',
[
[linjizong] 已提交
337 338 339 340 341 342 343 344 345 346 347
                style: TextStyle(color: Color(0xff333333), fontSize: 12)),
          ),
          Container(
            width: 80,
            alignment: Alignment.center,
            child: Text('${item.instancesCurrent}',
                style: TextStyle(color: Color(0xff333333), fontSize: 12)),
          ),
          Container(
              width: MediaQuery.of(context).size.width - 193,
              alignment: Alignment.center,
J
JellyBean 已提交
348
              child: Text('${item.classRef?.name}',
[
[linjizong] 已提交
349 350 351 352 353 354
                  style: TextStyle(color: Color(0xff333333), fontSize: 12))),
        ],
      ),
    );
  }
}